0

The output of my code is:

5! = 1 * 2 * 3 * 4 * 5 * = 120

How can I remove the last * to have this output:

5! = 1 * 2 * 3 * 4 * 5 = 120 
#include <iostream>
using namespace std;

int main(int argc, char** argv) {
            
    int n, count, factorial = 1;
    cout << "Enter a positive integer: ";
    cin >> n;
    cout << n << "! = ";
    
    if (n < 0){
        cout << "Error! Factorial of a negative number doesn't exist.";
        }
    else{
        while(count < n){
        count++;
        factorial = factorial * count ;
        cout << count << " * ";  
        }
            cout << " = " << factorial;
    }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Anonymous
  • 1
  • 2
  • 2
    By the way `count` is not initialised in your program. Formally the behaviour is undefined. Personally I'd hardcode "1", start the `count` from 2, and write `cout << " * " << count;` Also take care not to overflow `int`. – Bathsheba Oct 15 '21 at 07:19
  • Possible duplicate https://stackoverflow.com/questions/47107734/i-dont-want-a-comma-after-the-last-number-value-how-do-i-remove-it-c-loop – Galik Oct 15 '21 at 07:53
  • Possible duplicate https://stackoverflow.com/questions/22702736/for-loop-prints-an-extra-comma – Galik Oct 15 '21 at 07:55

2 Answers2

2

Yes add an if that checks if you're not on your last number. (Also don't use using namespace std, Why is "using namespace std;" considered bad practice?)

#include <iostream>

int main(int argc, char** argv)
{

    int n = 0;
    int count = 0;
    int  factorial = 1;

    std::cout << "Enter a positive integer: ";
    std::cin >> n;
    std::cout << n << "! = ";

    if (n < 0)
    {
        std::cout << "Error! Factorial of a negative number doesn't exist.";
    }
    else
    {
        while (count < n)
        {
            count++;
            factorial = factorial * count;
            std::cout << count;

            // only show * if not on last number
            if (n != count) std::cout << " * ";
        }
        std::cout << " = " << factorial;
    }
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
  • How can `n` be equal to `count` inside a `count < n` while-condition? :-) – Dominique Oct 15 '21 at 07:05
  • @Dominique I decided to keep the original loop code intact, the reason is the increment of count directly after the check. Which avoids multiplying by 0, but changes conventional logic somewhat ;) – Pepijn Kramer Oct 15 '21 at 07:13
  • You're right: I'm so used only increasing the looping index at the end that I completely overlooked it :-) – Dominique Oct 15 '21 at 07:28
  • My initial output was also off by one (good thing I test before I post). So yes its funny to realize there are "conventions" in writing loops too. – Pepijn Kramer Oct 15 '21 at 07:34
0

Your code, prettied up a little bit:

    else {
        while (count < n) {
            count++;
            factorial += count ;
            cout << count << " * ";  
        }
            
        cout << " = " << factorial;
    }

Now, let's put that " * " separator into a variable.

    else {
        string sep = " * ";

        while (count < n) {
            count++;
            factorial *= count ;
            cout << count << sep;  
        }
            
        cout << " = " << factorial;
    }

Still the same result, but let's try this:

    else {
        string sep = " * ";

        while (count < n) {
            count++;
            factorial *= count ;
            cout << sep << count;  
        }
            
        cout << " = " << factorial;
    }

Now we get the extra " * " in front of the first number. It'd work a lot better if sep were "" on the first iteration, and then we changed it to " * " on every other iteration.

    else {
        string sep = "";

        while (count < n) {
            count++;
            factorial *= count ;
            cout << sep << count;  
            sep = " * ";
        }
            
        cout << " = " << factorial;
    }
Chris
  • 26,361
  • 5
  • 21
  • 42