+ 1

How to code factorial using while loop?

15th Aug 2019, 9:06 AM
maxiel
maxiel - avatar
4 Answers
0
You have not indicated which programming language you are using to find factorial of a given number. If you want to use the C language, you can get the factorial with the following code (you may use the same logic for some other programming language): #include<stdio.h> #include<conio.h> void main() { int n,i,f; f=i=1; clrscr(); printf("Enter a Number to Find Factorial: "); scanf("%d",&n); while(i<=n) { f*=i; i++; } printf("The Factorial of %d is : %d",n,f); getch(); }
18th Aug 2019, 8:34 AM
Snehal
+ 4
int n = 1; int i = 0; int m = 10; //should be input while(i < m){ n *= i; i++; } cout<<n; with a for loop it would be a little shorter and easier to understand: int n = 1; int m = 10; //should be input for(int i = 0; i < m; i++){ n *= i; } cout<<n
15th Aug 2019, 9:17 AM
Anton Böhler
Anton Böhler - avatar
18th Aug 2019, 2:10 PM
Avinash
Avinash - avatar
0
thank you for answering
30th Aug 2019, 9:27 AM
maxiel
maxiel - avatar