(Write the algorithm and the code) Design a program that promps the user to inte
ID: 3622191 • Letter: #
Question
(Write the algorithm and the code)
Design a program that promps the user to inter a positive integer. Use a for loop to calculate and display the factorial value of that number. Increment the counter in your for loop. The factorial value of a anumber is a number multiplied by every factor between 1 and the number, inclusive. For instance, 3 factorial is 3*2*1. If the user enters a negative number,display a massage indicating that the program calculates only positive numbers. Otherwise,display the result of the calculation.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <cmath>
using namespace std;
int main()
{int n,i,fact=1;
cout<<"enter a number whos factorial you want to find: ";
cin>>n;
if(n<0)
cout<<"Can only find the factorial of a non negative number ";
else
{for(i=n;i>=2;i--)
fact*=i;
cout<<n<<" factorial= "<<fact<<endl;
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.