116k views
1 vote
C++

1) Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.

2) Write a nested loop that displays 10 rows of '#' characters. There should be 15 '#' characters in each row.

1 Answer

3 votes

Final answer:

To write a C++ program that meets the requirements, you can use a for loop to iterate 10 times and keep a running total of the numbers entered by the user. To display 10 rows of '#' characters with 15 '#' characters in each row, you can use a nested for loop.

Step-by-step explanation:

To write a C++ program that meets the requirements mentioned:

1) Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.

To achieve this, you can use a for loop that iterates 10 times. Inside the loop, prompt the user to enter a number using cin, and add the entered number to a variable that keeps track of the running total.

#include <iostream>

using namespace std;

int main() {
int total = 0;

for (int i = 0; i < 10; i++) {
int num;
cout << "Enter a number: ";
cin >> num;

total += num;
}

cout << "Running total: " << total << endl;

return 0;
}

2) Write a nested loop that displays 10 rows of '#' characters. There should be 15 '#' characters in each row.

To accomplish this, you can use a nested for loop. The outer loop controls the number of rows (10), and the inner loop controls the number of '#' characters in each row (15). Inside the inner loop, print '#' to display the characters in each row.

#include <iostream>

using namespace std;

int main() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 15; j++) {
cout << "#";
}
cout << endl;
}

return 0;
}

User Temi
by
7.7k points