0

I want to print the factorial of a number slightly differently. I am very new to c++ so I would like to have some help here. Suppose I give the input as 5 it means I will get 120 as my output, if the logic is right. But what I want as my output is as like this 1 2 6 24 120

Like first f * i then again f * i like that. I am a bit confused with the logic so please someone help me.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ofstream fact("factorial.txt");
    int i,n,f=1;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        f=f*i;
        fact<<i<<" ";
    }
    fact<<endl;
    fact<<f<<" ";

    return 0 ;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54

1 Answers1

0

Adding the f in the loop would do the trick; I also added some more text

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    cout << "Please Enter a Number for calculation: ";
    ofstream fact("factorial.txt");
    int i,n,f=1;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        f=f*i;
        fact<<f<<" ";
    }
    fact<<endl;
    fact<<f<<" ";
   cout << "The result is:" << f;
   cout << "\nYou can check the process in the Factorial.txt";
    return 0 ;
}

Also you might consider having a look into this article; as it will help you in the future when working with C++. Why is "using namespace std" a bad practice.

Hasan Patel
  • 412
  • 7
  • 19