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

12 - Write a program c++ to compute the sum of the integers that divisible by 4 from 10…

  1. User

    12

    Write a program c++ to compute the sum of the integers that divisible by 4 from 10 different integers (using DO/WHILE loop)

  2. Assistant

    Here's a C++ program that computes the sum of integers divisible by 4 from 10 different integers using the DO/WHILE loop:

    #include <iostream>
    
    int main() {
        int count = 0;
        int sum = 0;
        int num;
        
        // Loop to input 10 integers
        do {
            std::cout << "Enter an integer: ";
            std::cin >> num;
            
            // Check if the number is divisible by 4
            if (num % 4 == 0) {
                sum += num;
                count++;
            }
            
        } while (count < 10);
        
        std::cout << "Sum of integers divisible by 4: " << sum << std::endl;
        
        return 0;
    }
    

    In this program, we use a do-while loop to input 10 integers from the user. If the entered number is divisible by 4 (i.e., num % 4 == 0), we add it to the sum variable and increment the count variable. Once we have collected 10 integers, the loop exits, and we print the sum of integers divisible by 4.