gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Create a C++ program that prints even numbers from 1 to 100
- User
Create a C++ program that prints even numbers from 1 to 100
- Assistant
Here's a simple C++ program that prints even numbers from 1 to 100:
#include <iostream> int main() { for (int i = 1; i <= 100; i++) { if (i % 2 == 0) { std::cout << i << " "; } } return 0; }This program uses a
forloop to iterate from 1 to 100. For each number, it checks if it is divisible by 2 (i.e., an even number) using the modulo operator%. If it is, then it prints the number to the console usingstd::cout. The output is all the even numbers separated by a space.