Help please

how generate a Factorial Chart from 1 to N?

N = 5 N = 9 test your program.

sample run is

Enter a value for N: 6

Factional chart from 1 to 6

1! = 1
2! = 1 x 2
3! = 1 x 2 x 3
4! = 1 x 2 x 3 x 4
5! = 1 x 2 x 3 x 4 x 5
6! = 1 x 2 x 3 x 4 x 5 x 6

how can i get that? please help.

What do you know about loops? What code do you have?

Use either of the following for an idea of how to put your code into the forum:
http://www.cplusplus.com/articles/jEywvCM9/
or
http://www.cplusplus.com/articles/z13hAqkS/
my sample code is

int N;

cout <<"Enter a value for N: ";
cin >>N;

for (N=1; N<=9; N++)
cout <<N;
i don't know about loops so that's why i need help.
You wouldn't want to use N again for your for loop. You'd want to start off like this:

1
2
3
4
5
6
7
8
9
10
11
12
//declare variable
int n = 0;

//get input item
cout << "Enter a value for n: ";
cin >> n;

//loop
for (int i = 0; i < n; i++)
{
     //other code
} //end for 
@janineee29

Please take a look at this example, understand and try to make it better. This is only way to learn and enjoy programming...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main() {
	int i = 1, n;
	string str1, str2 = "";
	cout << "This program generates a Factorial Chart from 1 to n.";
	cout << "\n\nEnter a positive integer : ";
	cin >> n;
	if(n <= 0) return 1;//test for n to be positive
	cout << '\n';
	
	//these 4 lines generate the first line of the Chart
	str1 = to_string (i);// converts int to string (c++11 standard)
	cout << "   " << str1 + "! = ";// concatenates 2 strings & prints 
	                               // everything before the equal sign
	str2 += str1;//prepares str2 for print & while loop
	cout << str2 << '\n';//prints everything after the equal sign
	
	//this loop generates the next n-1 lines of the Chart
	//these 4 line increment i (as long as it remains less than n)
	while(i < n) {
		str1 = to_string (++i);//preincrements i then converts it to string
		cout << "   " << str1 + "! = ";//see the line 16
		str2 += " x " + str1;//adds the new "factor" to the old str2 
		cout << str2 << '\n';//prints everything after the equal sign
	}
	return 0;
}	
That might be a bad example... Im not sure but I think changing the int variable into a string variable changes the value of the number. From what I understand your changing an int into a number viewed for its ascii value when you change it into a string and that could create a problem all by itself. I might be wrong but I think it would be better to stick with an int variable that changes its value by multiplying its self by the next factorial which would be determined by the loop.

******************************************************************************************

This was written before I notice that the code wasn't written by the person searching for the solution. The only reason I'm including it is because it might help both of you... If I'm wrong someone please tell me.....

So, @condor, <--(initially this was not intended for you but after recognizing it wasn't created by @janineee29, I put your name in it too.. )

Before I say anything about your code, I want you to know your not doing a bad job at all. Although some of the tools your using for the problem at hand, seem to be used in a manor that I consider, unorthodox. (By tools I mean loops are a tool for programming that create a reoccurring pattern, int/string/double & so on are tools of programming to store input/data and if statements are a way to control the structure of a prog.: if (this is/is not what I want) { Do something else that I want...} ) Although, sometimes in programming being unorthodox is not only beneficial but essential, I don't believe this is one of those cases.

What's the purpose of the string?

My opinion:
-Unless your teacher is requiring a string to be used , which was not mentioned, I don't think you need one. If its part of the assignment ok, but if not ask your self why a string. The reason Im having you ask yourself stuff like this is because you need to view everything as a way to manipulate and store data. ---(Also, @condor, you forgot the header.)

Another thing, you need to know how some of these tools work before you try to use them. By converting the int's into strings, from my understanding, you would be converting what was a numerical integral value to the value provided via the ascii chart. Now if your intent is to use the string as a way of creating an array why not just make an array of int type? If anything Im saying is confusing just ask...

Now, lets look at the problem at hand and see what it is that your trying to do:
how generate a Factorial Chart from 1 to N?


Here is an example of how I feel you would find the best result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int factorial;

cout<<"Enter the number you want the factorial for: ";
cin>>factorial;
cout<<endl;

cout<<factorial<<" ";

for(int i = factorial, i > 0, --i){
    factorial = factorial * i;
    cout<<faactorial<<" ";
}
    cout<<endl;


*** I might have done something wrong in the math part but still it should be close... Also if you want to store each factorial separately with an array then try an int array instead of a string. Any questions about int arrays just ask..


If any of this repeats its because I made the mistake of reading the code of condor and thought it was the code of @janineee29. Sorry...

Hope this helps and if I am wrong someone anyone please tell me.
Last edited on
@OP:

Here you go.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    int a, i, j;
    cout << "Generate factorial chart up to: ";
    cin >> a;

    for (j = 1; j<=a; j++)
    {
        cout << j << "!=";
        for (i=1; i<=j; i++)
        {
            cout << i;
            if (i<j) cout << " x ";
        }
        cout << endl;
    }
    return 0;
}


I started a few days ago myself, so the code might be inefficient.
Try your best to improve it!

If you want to challenge yourself a bit, try giving the value of each factorial.

@Aim4Erudite

Your for loop is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
int factorial;

cout<<"Enter the number you want the factorial for: ";
cin>>factorial;
cout<<endl;

cout<<factorial<<" ";

for(int i = factorial, i > 0, --i){
    factorial = factorial * i;
    cout<<factorial<<" ";
}
    cout<<endl;

But anyway, if you fix it you'll get:

Sample input: 5
Sample output:
5 25 100 300 600 600
Last edited on
sorry I was thinking of the actual result I should have paid more attention to your example.
@supernoob thank you for the answering you're right :D but i solve this problem last 2 days :)) but anyway thank you guys :)
Topic archived. No new replies allowed.