Please help with c++ homework

I was just introduced to while loops but the instructions on this so-called "tutorial" I was assigned are extremely vague and unhelpful. I understand this is a very large wall of text, so if you only want to help with 1 step that is perfectly fine; I hate being a freeloader...

Any and all help in the following code is very much appreciated:


#include <iostream>
using namespace std;

int main()
{
	// This variable 'n' will be used repeatedly for how far any loop will count
	// You may define additional variables as necessary when solving the problems.
	int n;

	// These variables will hold the sums and products of selected values
	int sum, product=0;

	cout << "Please enter a small positive integer:  ";
	cin >> n;

	// The first couple of these operations will be repeated for various values of n
	while (n > 0)
	{
		// STEP 1:
		// Write a loop that displays the sum of the first 'n' integers
		// For example, the sum of the first three integers is 6.



		cout << "The sum of the first " << n << " integers is " << sum << "." << endl;

		// STEP 2:
		// Write a loop that displays the first 'n' integers that are NOT multiples of 3
		// For example, the first 5 such numbers are:  1 2 4 5 7
		// HINT: The largest such integer will be just less than  1.5*n
		// Display all these values on a single line, separated by spaces.




		cout << endl;		// end the line After all the values display

		// STEP 3:
		// Write a loop that displays the sum of the first 'n' odd integers
		// The largest such integer will be just less than 2*n
		// FOr partial credit, it will suffice to get the correct sum
		// For full credit, do so without using any if statements to test a number is odd



		
		cout << "The sum of the first " << n << " odd integers is " << sum << "." << endl;

		// STEP 4:
		// Complete the loop that started on line 36 by prompting for
		// and accepting a new input value.  Tell the user how to stop!


		n = 0;
	}

	// STEP 5:
	// Test the code that you have so far

	// STEP 6:
	// For any positive integer n, n factorial (written as 'n!' in math)
	// is the product of all the positive integers up to and including n.
	// Write a program loop that displays the first nine factorials
	// The first three lines of    1! = 1
	// output should look like     2! = 2
	// those at the right here     3! = 6
	// For full credit, do so with a single loop (no nested loops)


	// STEP 7:
	// Now produce the same output, but in reverse order, starting with 9 factorial
	// Again, for full credit, do so with a single loop (no nested loops)
	// Hint:  you may use the results from the end of the previous loop


	// STEP 8:
	// Test your results of the code you have so far.
	// The factorial answers should match each other.

	
	return 0;
}

Last edited on
Here you go buddy

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>

using namespace std;

int i;

int main()
{

// This variable 'n' will be used repeatedly for how far any loop will count
// You may define additional variables as necessary when solving the problems.

int n;

// These variables will hold the sums and products of selected values

int sum, product=0;

cout << "Please enter a small positive integer: ";
cin >> n;

// The first couple of these operations will be repeated for various values of n

while (n > 0)
{

// STEP 1:
// Write a loop that displays the sum of the first 'n' integers
// For example, the sum of the first three integers is 6.

sum=0;

for (i=1;i<=n;i++)
	sum+=i;

cout << "The sum of the first " << n << " integers is " << sum << "." << endl;

// STEP 2:
// Write a loop that displays the first 'n' integers that are NOT multiples of 3
// For example, the first 5 such numbers are: 1 2 4 5 7
// HINT: The largest such integer will be just less than 1.5*n
// Display all these values on a single line, separated by spaces.

for (i=1;i<=1.5*n;i++)
	if(i%3!=0)
		cout << i << " ";

cout << endl;	 // end the line After all the values display

// STEP 3:
// Write a loop that displays the sum of the first 'n' odd integers
// The largest such integer will be just less than 2*n
// FOr partial credit, it will suffice to get the correct sum
// For full credit, do so without using any if statements to test a number is odd

sum=0;

for (i=1;i<2*n;i+=2)
	sum+=i;

cout << "The sum of the first " << n << " odd integers is " << sum << "." << endl;

// STEP 4:
// Complete the loop that started on line 36 by prompting for
// and accepting a new input value. Tell the user how to stop!

cout << "Please give a new value. If you want to stop give 0." << endl;

cin >> n;

cout << endl;

}

// STEP 5:
// Test the code that you have so far

// STEP 6:
// For any positive integer n, n factorial (written as 'n!' in math)
// is the product of all the positive integers up to and including n.
// Write a program loop that displays the first nine factorials
// The first three lines of 1! = 1
// output should look like 2! = 2
// those at the right here 3! = 6
// For full credit, do so with a single loop (no nested loops)

product=1;

for(i=1;i<=9;i++)
{
	product=product*i;
	cout << i << "! = " << product << endl;
}

cout << endl;

// STEP 7:
// Now produce the same output, but in reverse order, starting with 9 factorial
// Again, for full credit, do so with a single loop (no nested loops)
// Hint: you may use the results from the end of the previous loop

cout << i-1 << "! = " << product << endl;

for(i=9;i>=2;i--)
{
	product=product/i;
	cout << i-1 << "! = " << product << endl;
}

cout << endl;

// STEP 8:
// Test your results of the code you have so far.
// The factorial answers should match each other.

return 0;

}
Thank you very much! Is there any way to turn those for loops into while loops?
The first part of the things in the parentheses () of the for loop should go before the loop, second part should be the condition for the while loop and the last part should be a command at the end inside of the while loop.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(i=9;i>=2;i--)
{
	product=product/i;
	cout << i-1 << "! = " << product << endl;
}

//is 

i=9;
while(i>=2)
{
        product=product/i;
	cout << i-1 << "! = " << product << endl;
        i--;
}
Topic archived. No new replies allowed.