EX: How to print factorial of the given number. What is the logic to print factorial of the given number. C Program to find the factorial of given number.
Input from user:
Enter the number: 7
Expected output:
Factorial is 5040
Logic to print factorial of the given number:
1. Accept input number from user declare variable say no.
2. make i=no.
3. We all know factorial of 0 is 1 so if no is equal equals to 0 print factorial is 1.
3. We all know factorial of 0 is 1 so if no is equal equals to 0 print factorial is 1.
4. Otherwise use while loop from no to (1+1). And inside that while loop use factorial number simple logic as follows:
while(no>1)
{
i=i*(no-1);
no--;
}
{
i=i*(no-1);
no--;
}
5. Last print value of i means factorial of given number.
#include<stdio.h>
void main()
{
int no,i;
printf("enter the number\n");
scanf("%d",&no);
i=no;
if(no==0)
{
printf("factorial is 1");
}
else
{
while(no>1)
{
i=i*(no-1);
no--;
}
printf("factorial is %d",i);
}
}
Above program shows the following output:
C program to print Factorial Of Given Number:
#include<stdio.h>
void main()
{
int no,i;
printf("enter the number\n");
scanf("%d",&no);
i=no;
if(no==0)
{
printf("factorial is 1");
}
else
{
while(no>1)
{
i=i*(no-1);
no--;
}
printf("factorial is %d",i);
}
}
Above program shows the following output:
No comments:
Post a Comment
If you have any doubts, please discuss here...👇