a program to calculate the factorial value of the inputted number. Use the increment formula ( i++ ) for your solution . Apply the three looping statements for your solutions:
Sample input/output dialogue:
Enter a number: 5
Factorial value: 120
(The computation is: 1*2*3*4*5=24)
1st Solution – using for loop statement
2nd Solution – using while loop statement
3rd Solution- using do while loop statement
#include <iostream>
#include <string>
using namespace std;
int main(void){
int positiveInteger;
cout<<"Enter a number: ";
cin>>positiveInteger;
int fact = 1;
//1st Solution – using for loop statement
for (int j = 1; j <= positiveInteger; j++) {
fact = fact * j;
}
cout<<"Factorial value: "<<fact<<"\n\n";
//2nd Solution – using while loop statement
fact = 1;
int j=1;
while(j <= positiveInteger) {
fact = fact * j;
j++;
}
cout<<"Factorial value: "<<fact<<"\n\n";
//3rd Solution- using do while loop statement
fact = 1;
j=1;
do{
fact = fact * j;
j++;
}while(j <= positiveInteger);
cout<<"Factorial value: "<<fact<<"\n\n";
system("pause");
return 0;
}
Comments
Leave a comment