(for)(do)(while) loops using Xcode

Write a C++ program that will input from the user a positive number n and find its factorial. Don’t forget to validate the input. The factorial of a positive integer n (denoted by n!) is defines as the product of the integers from 1 to n. n! = 1* 2 * 3 * ... * (n - 1) * n
You should allow the user to continue working with your program for additional data sets.


Sample output:

Please enter a number: 5
5! = 120
Would you like to continue (Y/N)?Y
Please enter a number: 3
3! = 6

Would you like to continue (Y/N)?N
Good Bye!!


I have a basic idea of what to do but I'm missing a step somewhere and I would like to learn what I am doing wrong if someone can post either steps or a code so i can base off it would be helpful. my code for what i think I'm doing is as follows


#include <iostream>
using namespace std;
int main()
{
int i=1;
int n;
char ans;
int factorial;


cout<< "Would you like to enter a number ?(Y/N)"<< endl;
cin>>ans;


cout<< "Please enter a number"<< endl;
cin>> n;
while (i<=n) {
do {
factorial *= i;
i++;
cout<<n<<"!="<<factorial<< endl;
cout<< "Would you like to enter a number ?(Y/N)"<< endl;
cin>>ans;
} while ((ans =='y')||(ans =='Y'));

}




return 0;
}
still learning everything so please let me know if completely botched something
Last edited on
Why don't you post your code, and we can help point out where you are going wrong.
Topic archived. No new replies allowed.