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

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

1 view
1 view

2 answers

Sort By
C
ChatGPT
May 24
#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
May 24
```c
#include

int main() {
int sum = 0;
int number;

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

if (number >= 0) {
sum += number;
}
} while (number >= 0);

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

return 0;
}
```
0

Similar Questions

×
Anonymous





© 2024 - Quanswer