I am having difficulties writing the program.
the program should ask user for a positive integer and print a factorial table.

for example -

Give me a positive integer: 5
1! = 1 = 1
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
5! = 5 x 4 x 3 x 2 x 1 = 120

    int s, factorial = 1;
    cout << "Enter a positive integer: ";
    cin >> s;
    while (s < 0)
    {
        cout << "Enter positive integer only: ";
        cin >> s;
    }
    for (int r = 1; r <= s; r++)
    {
        factorial *= r;

        for (int c =1; c <= r; c++)
        {
            // what goes here?
        }
        cout << endl;
    } 

need some clues.

Recommended Answers

All 6 Replies

you don't need two nested loops, just one will do that counts backwards from 's' to 1. Inside the loop you just need to print the value of the loop counter and 'X' as well as calculate the factorial of s.

my version of your factorial is this:

    int s, factorial = 1;

    std::cout << "Enter a positive integer: ";

    while ( std::cin >> s && s < 0 )
        std::cout << "Enter positive integers only: ";

    for ( int i = 2; i <= s; ++i )
        factorial *= i;

    std::cout << factorial << std::endl;

I need to print it exactly like the example I mentioned..if you look at my code you can see that I got the factorial output right...but I couldn't get it like this
3! = "3 x 2 x 1" = 6

I'm missing the middle part " "

I'm missing the middle part " "

print quotation marks before and after the loop/process that does the operation

your program is missing three parts, not just one as shown in the comment on line 15.
1. Before line 13 display the value of r and the '!=' so that if r == 1 it woould display "1!=".
2. Inside the loop at line 15 display the value of c and if c != r then also display 'X'.
3. The third piece is after the end of the loop, between lines 16 and 17, you need to display the value of the computed factorial.

include1_page-0001.jpg

This post has no text-based content.
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.