A C program to find the factorial value using while loop:
- In mathematics , the factorial of positive integer n! , that is n!=n x (n-1) x (n-2) x (n-3) .......... x 3 x 2 x 1 .
Now , we will do this through the C using while loop . In C programming , we can do this various way . But we only run the program only while() loop .
Here is the source code :
#include<stdio.h>
int main()
{
int x,y=1;
printf("Enter any integer :");
scanf("%d",&x);
if(x<0)
printf("Factorial of negative number does not exist .");
else{
while(x>=1)
{
y=y*x;
x--;
}
printf("The factorial of the above number is :%d",y);
}
return 0;
}
Let us understand the above program with an example,
we consider an integer 6 .
Using while() loop,
when x=6(Input)
loop will be started after giving input .
y= 6*1(as y=1)
next loop ,
y= 6*(6-1) that is , y=6*5
next loop,
y=30*(5-1) that is , y=30*4
next loop,
y=120*(4-1) that is , y=120*3
next loop ,
y=360*(3-1) that is , y=360*2
next loop,
y=720*(2-1) that is , y=720*1
At this time , the loop will stop , as per condition .
Tags:
Programming
Share:
factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
ReplyDeletefactorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
ReplyDeleteauto-accident-compensation-claims-in-michigan When needing to hire a car accident lawyer, you want someone you can trust to see your case through to the end.
car-accident-lawyer-san-fernando-valley When needing to hire a car accident lawyer, you want someone you can trust to see your case through to the end.