Help Needed With Loops

So I'm about one month into my C++ class and we just started learning about loops but for the life of me I cant wrap my head around how to write the code the professor has asked of. I know its the most basic of things but its like a foreign language to me. I understand what each loop does but cant seem to think of the condition to set even the question, any help would be greatly appreciated

1.
Prompt the user to enter in a positive integer. Use a while loop to print out factors of 8 up to that
number of times that the user has entered. For instance, if the user enters 4, you would print out:
8
16
24
32
2.
Prompt the user to enter in a positive integer. Use a for loop to raise an integer 2 to that power.
For example: user enters 5, use a for loop to calculate 2^5, which is 32. Print out the final result.
(Hint: think about number of iterations carefully here. If you have 1, you won't want to do any
multiplications because 2^1 = 2.)
3.
Use nested for loops to print out the following "number pyramid."
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
4.
Print out the number pyramid in reverse:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
1) appears to be screwy wording. normally a factor is part of a multiplication to get the target, eg 2*4, 2 and 4 are factors of 8. 16 and 32 are NOT factors of 8, they are multiples. I think the prof meant multiples, is why its confusing.

Why not give them a try? The site has a tutorial on basic looping here:
https://www.cplusplus.com/doc/tutorial/control/

if you can't get it after giving it a good try, ask something specific that you don't understand.

Hint for (3) and (4): First, just focus on just printing one row.
e.g. can you print 5 4 3 2 1?
What makes it different than printing 4 3 2 1? Factor out the starting number as an input to a function.
1
2
3
4
for (...)
{
    printRow(i)
}

where printRow(i) will also contain a for loop.
I had a hard time with loops also, so here you go. Try the last one yourself (no.4).

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
#include <iostream>
using namespace std;

int main() {

    int a{};
    int b{};
    cout << "Enter in a positive integer: ";
    cin >> a;
    for (int i{ 0 }; i < a; i++) {
        b = b + (a * 2);
        cout << b << endl;
    }
    int c{ 0 };
    int d{ 1 };
    int e{ 2 };
    cout << "Enter in a positive integer: ";
    cin >> c;

    for (int i{}; i < c; i++) {
        d = d * e;
    }
    cout << d << endl;
    cout << "Without a loop\n" << pow(e, c) << endl;

    int f{};
    cout << "Print out the following number pyramid: ";
    cin >> f;
    for (int i{ 1 }; i <= f; i++) {
        for (int j{ 1 }; j < i; j++) {
            cout << j << " ";
        }
        cout << i << endl;
    }

    return 0;  
}
The requirement for 1) is to use a while. Also the logic isn't what was asked. Also for 2) the hint hasn't been used.

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

int main() {
	int a {};
	int b {};

	std::cout << "\n1) Enter in a positive integer: ";
	std::cin >> a;

	while (a--)
		std::cout << (b += 8) << '\n';

	std::cout << "\n2) Enter in a positive integer: ";
	std::cin >> a;

	int c {2};

	for (int i {1}; i < a; ++i, c <<= 1);
	std::cout << c << '\n';
}



1) Enter in a positive integer: 4
8
16
24
32

2) Enter in a positive integer: 5
32

Last edited on
Topic archived. No new replies allowed.