Factorial value of any integer by C language using while loop

 A C program to find the factorial value using while loop:

At first we have to know , what is factorial??
  • 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 .
For example , 6! = 6 x 5 x 4 x 3 x 2 x 1. However , 0!=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 .


Share:

2 Comments

For query , suggestion and others , please comment below. Don't share external link or spam on comment box . 💌

  1. 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.

    ReplyDelete
  2. 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.
    auto-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.

    ReplyDelete
Previous Post Next Post