Answer to Question #290493 in C++ for afgsgga

Question #290493

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

1
Expert's answer
2022-01-25T08:31:34-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment