C Program to find sum of two numbers



#include<stdio.h>
#include<conio.h>
void main()
{
int number,fact=1,num;
clrscr();
printf("Enter a number:");
scanf("%d",&number);
num=number;
while(number>0)
{
fact=fact*number--;
}
printf("Factorial of number %d is:%d",num,fact);
getch();
}

Explanation:

The above program will find out the factorial of the given number. The program takes an integer value from user and assigns that value in the variable 'number' and further it assigns a number one-less than the value present in number variable in another variable num. While loop will be executed till the number does not become less than 0.In the loop we will calculate the factorial of given number. We can understand this process by given example:

number=4,fact=1
number>0,True
iteration 1:
fact=fact*number--=1*4=4
number=3
number>0,True
iteration 2:
fact=4*3=12
number=2
number>0,True
iteration 3:
fact=12*2=24
number=1
number>0,True
iteration 4:
fact=24*1=24
number=0
number>0,False
loop break
So factorial of number 4 is equal to 24.
The output of the above program is given below:

Output:

c program