C program to Calculate factorial

Below is C program to Calculate factorial .

Program: Calculate factorial using while loop

#include<stdio.h>
int main()
{
    int a,f,i;
    printf("Enter a number: ");
    scanf("%d",&a);
    f=1;
    i=1;
    while(i<=a)
    {
        f = f * i;
        i++;
    }
    printf("Factorial: %d",f);
 return 0;
}

Program: Calculate factorial using for loop

/* Calculate factorial using for loop */
#include<stdio.h>
int main()
{
    int a,f,i;
    printf("Enter a number: ");
    scanf("%d",&a);
    f=1;
    for(i=1;i<=a;i++)
        f = f * i;
    printf("Factorial: %d",f);
 return 0;
}

No comments:

Post a Comment