WSQ #6, Mastery Topic 14

--Originally published at TC1017 Programing Curse

This time i have to create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial), and then ask the user if he want to do it again.

here also we can se the mastery topic 14, the use of for

#include <iostream>
using namespace std;

int factorial (int n)
{
if (n == 0 ){
return 1;
} else {
int mults = 1;
for (int i = 1; i <= n; i++){
mults = mults * i;
}
return mults;
}

}
int main(){
int n;
char ans;
do{
cout << “Escribe un numero positivo ” << endl;
cin >> n;
for (int i = 0; i <= n; i++){
cout << i << ” factorial is ” << factorial(i) << endl;
}
cout << “Do you want another factorial y/n ” << endl;
cin >> ans;
}
while (ans == ‘Y’ || ans == ‘y’);
cout << “Have a nice day ” << endl;
return 0;
}

Resultado de imagen para loop

image taken from: http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.htmlwsq6 dowhile.PNG