Hey all, i'm having a bit of trouble with this - it's a program that should display the factorials of all numbers from 0 to k in the range 1-10. here's what i keep getting:
Enter a number to see factorials for: 10

10! = 100
9! = 90
8! = 80
7! = 70
6! = 60
5! = 50
4! = 40
3! = 30
2! = 20
1! = 10
The factorial of 10 is 3628800
Press any key to continue . . .


...and here is what it needs to look like:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800


Any help would be SO appreciated! Thanks all :)

#include<iostream>
using namespace std;

int factorial(int);

void main()  
{  
  
    int x;  
    int factorial=1;  
      
    cout << "Enter a number to see factorials for: ";  
    cin >> x;  
  
   cout << "\n";  
    for (int i = x; i; i--)  
    {  
        cout << i << "!"<< " = " << (i*x)<<endl;  
        factorial *= i;  
    }  
  
    cout << "The factorial of "<<x<<" is "<<factorial<<endl;  
  

}  

int factorial(int n) 
{
     if (n <= 1) return 1;
     return n * factorial(n-1);
}

You're mixing up two different solutions for calculating a factorial. Just remove all of the manual stuff and use the function:

#include<iostream>
using namespace std;

int factorial(int);

int main()  
{
  int x;  

  cout << "Enter a number to see factorials for: ";
  cin >> x;

  cout << "\n";
  for (int i = 0; i <= x; i++)
  {
    cout << i << "!"<< " = " << factorial(i) <<endl;
  }
}

int factorial(int n)
{
  if (n <= 1) return 1;
  return n * factorial(n-1);
}

You're mixing up two different solutions for calculating a factorial. Just remove all of the manual stuff and use the function:

#include<iostream>
using namespace std;

int factorial(int);

int main()  
{
  int x;  

  cout << "Enter a number to see factorials for: ";
  cin >> x;

  cout << "\n";
  for (int i = 0; i <= x; i++)
  {
    cout << i << "!"<< " = " << factorial(i) <<endl;
  }
}

int factorial(int n)
{
  if (n <= 1) return 1;
  return n * factorial(n-1);
}

I see! The code works beautifully, thank you.
However, I'm still a little confused as to how I should implement a range with these factorials - it's supposed to be a number only between one and ten. I've implemented an if statement after the first cout to make sure - but then it doesn't do anything if I enter a number that IS in the range.
Here's where the problem is:

if (x<10)
{
for (int i = 0; i <= x; i++)
cout << i << "!"<< " = " << factorial(i) <<endl;
}
else
cout << x << " is not in the range - try again : ";
cin >> x;

>but then it doesn't do anything if I enter a number that IS in the range
I assume you mean that it doesn't do anything after printing the factorials. It doesn't do anything because it's waiting for input. And it's waiting for input because you forgot to add braces to the else clause:

if (x<10)
{
  for (int i = 0; i <= x; i++)
    cout << i << "!"<< " = " << factorial(i) <<endl;
}
else 
{
  cout << x << " is not in the range - try again : ";
  cin >> x;
}

However, that still won't be effective because unless you've wrapped this whole thing in a loop, reading another value into x is pointless. The program will just end as normal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.