ALGO
The algorithm for the factorial is:
Program
The algorithm for the factorial is:
- input a number n
- set variable final as 1
- final <= final * n
- decrease n
- check if n is equal to 0
- if n is equal to zero, goto step 8 (break out of loop)
- else goto step 3
- print the result final
flowchart
Program
/* Source code to find factorial of a number. */
#include<iostream>
using namespace std;
int factorial(int n);
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}
No comments:
Post a Comment