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

Create a C++ program that prints even numbers from 1 to 100

  1. User

    Create a C++ program that prints even numbers from 1 to 100

  2. 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 for loop 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 using std::cout. The output is all the even numbers separated by a space.