Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

11-Write a program c++ to compute the sum of even nu mbers from some numbers until "0"…

  1. User

    11-Write a program c++ to compute the sum of even nu mbers from some numbers until "0" by using DO/WHILE loop

  2. Assistant

    Here is an example of a C++ program that computes the sum of even numbers from a series of numbers until "0" using a do-while loop:

    #include <iostream>
    using namespace std;
    
    int main() {
       int num, sum = 0;
       
       do {
          cout << "Enter a number: ";
          cin >> num;
    
          if (num % 2 == 0) {
             sum += num;
          }
       } while (num != 0);
    
       cout << "Sum of even numbers: " << sum << endl;
    
       return 0;
    }
    

    In this program, we use a do-while loop to repeatedly ask the user to enter a number. If the entered number is even (i.e., divisible by 2 without a remainder), we add it to the sum variable. The loop continues until the user enters 0, at which point the loop exits and the final sum of even numbers is displayed.