# C++ Factorial, Prints the Equation, How to print Answer?

**URL:** <https://forum.codewithmosh.com/t/c-factorial-prints-the-equation-how-to-print-answer/23471>\
**Category:** Uncategorized\
**Created:** [November 6, 2023, 2:45pm UTC](https://forum.codewithmosh.com/t/c-factorial-prints-the-equation-how-to-print-answer/23471 "2023-11-06T14:45:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuspiciousShrimp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/suspiciousshrimp/32/7328_2.png) [@SuspiciousShrimp](https://forum.codewithmosh.com/u/SuspiciousShrimp)\
**Post date:** [November 6, 2023, 2:45pm UTC](https://forum.codewithmosh.com/t/c-factorial-prints-the-equation-how-to-print-answer/23471/1 "2023-11-06T14:45:32Z")

</div>

So I am doing C++ course, and Mosh made an answer with answer and not equation for the factorial exercise, how can I implement answer to in this program with only few changes?

```auto
// Online C++ compiler to run C++ program online
#include <iostream>

using namespace std;

int main() {
    int factorial;
    cout << "Number(Positive Please): ";
    cin >> factorial;

    if (factorial < 0) 
        cout << "Not Positive! ";
    
    if (factorial == 0) cout << 1;
    else {
        for (int i = factorial; i > 0; i--){
            cout << i;
            if (i == 1)
                cout << endl << "Done";
            else 
                cout << " * ";
        }
    }

    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![AJ\_KTS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/aj_kts/32/7051_2.png) [@AJ\_KTS](https://forum.codewithmosh.com/u/AJ_KTS)\
**Post date:** [November 7, 2023, 9:46am UTC](https://forum.codewithmosh.com/t/c-factorial-prints-the-equation-how-to-print-answer/23471/5 "2023-11-07T09:46:03Z")

</div>

Sorry for deleting the last three replies! I was trying(three times) to give you a fruitful solution.

I hope this solution would help you!

```auto
#include <iostream>

using namespace std;

int main() {
    int num;
    cout << "Number(Positive Please): ";
    cin >> num;

    if (num < 0) 
        cout << "Not Positive! ";
    
    if (num == 0) cout << 1;

    else { 
        int factorial = num;
        for (int i = factorial; i > 0; i--){
            if(i == 1)
                cout<< i;
            else{
                cout << i
                     << " * ";
                }
            }

        int finalOutput = 1;
        for (int i = 2; i <= num; ++i){
            finalOutput *= i;
         }

         if(finalOutput == 1)
         cout <<"*1 = "<< finalOutput<<endl;
         
         else { cout <<" = "<< finalOutput<<endl;}
         cout<< "Done";
    }
    return 0;
}

```
