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!

Recommended Answers

All 4 Replies

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.

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.

See my edit up above too. Glad it worked.

Okay. I will. Thank you very much :)

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.