Thursday 5 June 2014

0

C Program 15: To Find the Factorial of a number using for loop

  • Thursday 5 June 2014
  • IndianBooktuber
  • Share
  • Aim: To find factorial of a number
    Factorial of a number like 4 would be 4*3*2*1=24
    Following would be the code:
    // To find the factorial of a given number using for loop
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
         int fact, a;
         printf("Enter the number: ");
         scanf("%d", &a);
         fact=factorial (a);
         printf("The factorial of the given number is: %d\n", fact);
         getch();
         
    }
    int factorial(int x)
    {
        int f=1,i;
        for(i=x; i>=1; i--)
        {
                 f=f*i;
        } 
        return f;
    }

    Just a for loop for(i=x; i>=1; i--) is used. 

    In case, you want to do the same through recursion.. check this link:

    0 Responses to “C Program 15: To Find the Factorial of a number using for loop”

    Post a Comment

    Subscribe