Hi, I need a easier way to do a factorial program in c++ the program takes the i
ID: 3609961 • Letter: H
Question
Hi, I need a easier way to do a factorial program in c++ the program takes the input from the command line (only onenumber) and the number should be a 8-bit unsigned integer,however the calculation should be in 32-bit signed integer and thenthe result is printed out . the output should be an integer with no ( e ) notation ; forexample 4e8 is not a valid output. if the calculation is impossible the programs gives a massege( " Impossible to calculate " ). please I need the codes with explainations . . . Hi, I need a easier way to do a factorial program in c++ the program takes the input from the command line (only onenumber) and the number should be a 8-bit unsigned integer,however the calculation should be in 32-bit signed integer and thenthe result is printed out . the output should be an integer with no ( e ) notation ; forexample 4e8 is not a valid output. if the calculation is impossible the programs gives a massege( " Impossible to calculate " ). please I need the codes with explainations . . .Explanation / Answer
#include<iostream.h>
using namespace std;
int main(int argc, char * argv[])
{intnum;
int factorial=1,i=1,previous=0; //start factorialat multiplicative identity
//previous is previous value of factorial
num =atoi(argv[1]); //get character input from command prompt
//and convert to integer
while(i<=num&&previous<=factorial) //keepdoing it until find value you want or got overflow
{previous=factorial; //save factorial in previous, when next
factorial*=i; //value is too big it will overflow and become negative
i++;
}
if(i==num+1) //when this occurs the value was found
cout<<num<<"! ="<<factorial<<endl;
else
cout<<num<<"! is impossible to calculate ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.