C program to find factorial of a number- Using for loop

C program to find factorial of a number- Using for loop

c program to find factorial of a number using for loop. 

 #include<stdio.h>

#include<conio.h>

void main()

{

int n, f=1, i;

printf("\n Enter a number");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

f=f*i;

}

printf("\n factorial of %d = %d",n,f);

getch();

}

output





How factorial  is calculated 

Factorial of number is calculated by 

n*(n-1)*(n-2)*(n-3)....…..........................1

For example factorial 5 is

5*(5-1)*(5-2)*(5-3)*(5-4)

5*4*3*2*1

120

Here in program

User have to enter number to be find factorial the value is store in the variable n.

for(i=1;i<=n;i++)

The loop is continuous from 1 to n

f=f*i

It multiply number as 

1*(n-1)*(n-2)*(n-3)............n 

printf("\n factorial of %d = %d",n,f);

The part helps to print value of f which is factorial of n










Post a Comment

0 Comments