Ex: What is the logic to print factorial of 1 to n Number's. How to print factorial of 1 to n numbers. C program to print factorial of 1 to n number.
Input from user:
Enter the number: 5
Expected output:
Factorial of 1 is: 1
Factorial of 1 is: 2
Factorial of 1 is: 6
Factorial of 1 is: 24
Factorial of 1 is: 120
Logic to print factorial of 1 to n number's:
1. Accept input number from user.
2. Run outer for loop from 1 to no:
for(i=1;i<=no;i++)
3. Make f=i.
for(i=1;i<=no;i++)
3. Make f=i.
4. Run inner for loop from i to 2 and inside the inner for loop add given factorial logic:
for(j=i;j>1;j--)
{
f=f*(j-1);
}
5. After that outside the inner loop print value of f means factorial numbers.
Above Program show's the following output:
C Program To Print Factorial Of 1 to n Number's:
#include<stdio.h>
void main()
{
int i,j,no,f;
printf("Enter the number:\n");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
f=i;
for(j=i;j>1;j--)
{
f=f*(j-1);
}
printf("Factorial of %d is = %d\n",i,f);
}
}
Above Program show's the following output:
No comments:
Post a Comment
If you have any doubts, please discuss here...👇