+ 1
How to code factorial using while loop?
4 ответов
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();
}
+ 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
+ 2
0
thank you for answering