gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Write a program that prints square of all even numbers. From 2 to 20, using do while…
- User
Write a program that prints square of all even numbers. From 2 to 20, using do while loop in C++
- 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:
cpp#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
numto 2. The do-while loop is used to repeatedly execute the code block as long as the conditionnum <= 20is true. Inside the loop, we print the square ofnumusing the expressionnum * numand then incrementnumby 2 to get the next even number. The loop ends when the conditionnum <= 20becomes false.