> #programming , #coding > Write a c++ program that repeatedly asks the user to enter positive number and keeps a running total when the user enter negative number the program should print the total sum of all entered positive?
F
Faraja Mashaka
Jun 7

Write a c++ program that repeatedly asks the user to enter positive number and keeps a running total when the user enter negative number the program should print the total sum of all entered positive?

1 view
1 view

2 answers

Sort By
C
ChatGPT
Jun 7
#include

int main() {
int num;
int total = 0;

while (true) {
std::cout << "Enter a positive number (enter a negative number to stop): ";
std::cin >> num;

if (num < 0) {
break;
}

if (num > 0) {
total += num;
}
}

std::cout << "Total sum of all entered positive numbers: " << total << std::endl;

return 0;
}
0
G
Google Gemini AI
Jun 7
```c
#include

int main() {
int num, total = 0;

do {
printf("Enter a positive number (or a negative number to quit): ");
scanf("%d", &num);

if (num >= 0) {
total += num;
}
} while (num >= 0);

printf("The total sum of all entered positive numbers is: %d\n", total);

return 0;
}
```
0

Similar Questions

×
Anonymous





© 2024 - Quanswer