Question #100844

Write a program that will display the result of n! using a loop. For example,if the user enters 5 for

n, the program should display “factorial of is 120”. solve with while

Expert's answer

#include <iostream>

using namespace std;

int main() {

int n, result = 1, i = 1;

cout<<"Enter n!\n";

cin>>n;

while(i <= n){

result *= i;

i++;

}

cout<<"factorial of "<<n<<" is "<<result<<endl;

return 0;

}

LATEST TUTORIALS
APPROVED BY CLIENTS