How do I get rid of the zero digit in this program? this is a factorial problem and I have to show the numbers to be multiplied before arriving to the answer. The problem is, in the iteration process, the number zero will show. How do I get rid of it? thanks.

Here's my code:

#include <iostream>
#include <conio.h>

using namespace std;

int fac(int num);

int main ()
{
int num;

cout << "Enter a non-negative integer: ";
cin >> num;
cout << endl;
cout << num << "! = " << "(" << num << ")"<< "";
cout << "\n\nThe factorial of " << num << " is " << fac(num) << endl;

system("PAUSE");
return 0;
}

int fac(int num)
{
int factorial = 1;

for (int i = 1; i <= num; i++)
{
factorial *= i;
cout << "(" << num-i << ")" << "";

}
return factorial;
}

Thanks!

Change line 29 to:

if(i !=num) 
    cout << "(" << num-i << ")" << "";

Or better you could set factorial to num in your function and change your loop to go from for(i = num-1;i>=1;i--) and only display i.

Edited 6 Years Ago by jonsca: n/a

Change line 29 to:

if(i !=num) 
    cout << "(" << num-i << ")" << "";

It's not elegant but it works.

It worked. Hey, thank you very much! :)

See my edit up above too. Glad it worked.

Okay. I will. Thank you very much :)

This question has already been answered. Start a new discussion instead.