#WSQ6 factorial calculator

i was writing this blog a couple of days before, but i was in a rush and run out of time without finish it and don’t  really remember what i wanted to say about it, so i’ll reserve myself to put some pictures with maybe a simple explanation, some pics and the code Captura de pantalla (188)Captura de pantalla (189)

what i have done is jut make a function that calculates the factorial of a number and call it inside my main, this function need 1 income of data from the user to use it as a limit and know how far should it have to keep working, and then take two variables with value one and make a product betwen ponints, then one of those values take the value of the result and ads one to the another, and repeats the loop over and over and over again until the summed number reaches the limit number, and then return the multiplied number,  but it don’t take much time to realise that my program don’t work above fifteen, witch i think happens due to my program have to do a way to long process to keep running above that, and just returns a negative value.

as usual, some shots of how it works.

Captura de pantalla (191)

and also as usual here is write code because i know nobody is going to use it, but it’s a requirement of the task.

 

#include<iostream>
using namespace std;

int factorial(int factor)
{
int proceso=1;
int resultado=1;
while(proceso<=factor)
{
resultado=resultado*proceso;
proceso=proceso+1;
}
return resultado;
}

int main()
{
int primnum;
int secnum;
cout<<“please give me a number an i will give you the factorial of that number.”<<‘\n’;
cin>>primnum;
int primopera=factorial(primnum);
cout<<“the factorial of your number is:”<<primopera<<‘\n’;
char chose;
cout<<“if you want the factorial of another number please type y, if you dont please type n.”<<‘\n’;
cin>>chose;
while(chose!=’y’ and chose!=’n’)
{
cout<<“please type y or n to continue.”<<‘\n’;
cin>>chose;
}
while(chose==’y’)
{
cout<<“please writhe a new number.”<<‘\n’;
cin>>secnum;
int segopera=factorial(secnum);
cout<<“the factorial of your second number is:”<<segopera<<‘\n’;
cout<<“if you want the factorial of another number please type y, if you dont please type n.”<<‘\n’;
cin>>chose;

}
while(chose==’n’)
{
cout<<“well then, have a nice day n.n”<<‘\n’;
break;
}
return 0;
}

thanks for reading, and if you leave a comment, a like or share my blogpost i’ll be very grateful.

Leave a comment