14 - Write a program c++ to calculate positive n umbers till 50 only . If the user…
- User
14
Write a program c++ to calculate positive n umbers till 50 only . If the user enters a negative number, that number is skipped from the calculation
- Assistant
Here is a C++ program that calculates positive numbers up to 50, skipping any negative numbers entered by the user:
#include <iostream> int main() { int number; int sum = 0; std::cout << "Enter positive numbers till 50 (negative numbers will be skipped):\n"; do { std::cout << "Enter a number: "; std::cin >> number; if (number < 0) continue; sum += number; } while (number <= 50); std::cout << "Sum of positive numbers up to 50 is: " << sum << std::endl; return 0; }In this program, we use a
do-whileloop to continuously ask the user for input until the number entered is greater than 50. Inside the loop, we check if the number is negative using anifstatement. If it is negative, we use thecontinuestatement to skip the rest of the loop and move on to the next iteration. Otherwise, we add the number to thesumvariable.After the loop ends, we print the sum of all the positive numbers entered by the user up to 50.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.