Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Write a program that prints square of all even numbers. From 2 to 20, using do while…

  1. User

    Write a program that prints square of all even numbers. From 2 to 20, using do while loop in C++

  2. Assistant

    Here's an example program in C++ that prints the square of all even numbers from 2 to 20 using a do-while loop:

    #include <iostream>
    using namespace std;
    
    int main() {
        int num = 2;
    
        do {
            cout << num * num << " ";
            num += 2;
        } while (num <= 20);
    
        return 0;
    }
    

    In this program, we initialize the variable num to 2. The do-while loop is used to repeatedly execute the code block as long as the condition num <= 20 is true. Inside the loop, we print the square of num using the expression num * num and then increment num by 2 to get the next even number. The loop ends when the condition num <= 20 becomes false.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.