Monday, 19 September 2016

DS-LAB(FACTORIAL)exp-2

ALGO
 The algorithm for the factorial is:


  1. input a number n
  2. set variable final as 1
  3. final <= final * n
  4. decrease n
  5. check if is equal to 0
  6. if n is equal to zero, goto step 8 (break out of loop)
  7. else goto step 3
  8. 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