Open In App

Program to print first 10 even numbers

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Program to print first 10 even numbers. A number is even if it is divisible by 2 for example 4, 100, 24 etc.

Output Format:

0 2 4 6 8 10 12 14 16 18

Approach: Checking Parity using Modulo operator(%)

Using the modulo % operator we can find the remainder of any number when divided by 2, giving us the parity according to two cases:

  • remainder = 0: Even number
  • remainder = 1: Odd number

While we do not get first 10 even numbers, we can use the above method to check the parity and print the even numbers.

Step-by-step algorithm:

  • Create a function first10Even() which prints the first 10 even numbers.
  • Keep count of total even numbers printed using a variable cnt initialized to 0.
  • Until cnt reaches 10, iterate on whole numbers:
    • if a whole number is even print that whole number and increment cnt by 1.

C++




#include <iostream>
using namespace std;
 
void first10Even()
{
    int cnt = 0;
    int number = 0;
    while (cnt < 10) {
        if (number % 2 == 0) {
            cnt++;
            cout << number << " ";
        }
        number++;
    }
}
 
int main()
{
    cout << "First 10 even numbers are:\n";
    first10Even();
}


Java




public class First10Even {
    public static void main(String[] args) {
        // Display a message indicating the purpose of the program
        System.out.println("First 10 even numbers are:");
 
        // Call the method to print the first 10 even numbers
        first10Even();
    }
 
    // Method to print the first 10 even numbers
    static void first10Even() {
        // Initialize a counter for the number of even numbers and a variable to store the current number
        int cnt = 0;
        int number = 0;
 
        // Continue the loop until 10 even numbers are printed
        while (cnt < 10) {
            // Check if the current number is even
            if (number % 2 == 0) {
                // Increment the counter and print the even number
                cnt++;
                System.out.print(number + " ");
            }
 
            // Increment the number for the next iteration
            number++;
        }
    }
}


Python3




def first_10_even():
    # Initialize a counter for the number of even numbers and a variable to store the current number
    cnt = 0
    number = 0
 
    # Continue the loop until 10 even numbers are printed
    while cnt < 10:
        # Check if the current number is even
        if number % 2 == 0:
            # Increment the counter and print the even number
            cnt += 1
            print(number, end=" ")
 
        # Increment the number for the next iteration
        number += 1
 
# Display a message indicating the purpose of the program
print("First 10 even numbers are:")
 
# Call the function to print the first 10 even numbers
first_10_even()


C#




using System;
 
public class GFG {
    // Function to print the first 10 even numbers
    static void First10Even()
    {
        int cnt = 0;
        int number = 0;
        while (cnt < 10) {
            if (number % 2 == 0) {
                cnt++;
                Console.Write(number + " ");
            }
            number++;
        }
    }
 
    public static void Main()
    {
        Console.WriteLine("First 10 even numbers are:");
        First10Even();
    }
}


Javascript




function first10Even() {
    let cnt = 0;
    let number = 0;
    while (cnt < 10) {
        if (number % 2 === 0) {
            cnt++;
            process.stdout.write(number + " ");
        }
        number++;
    }
}
 
console.log("First 10 even numbers are:");
first10Even();


Output

First 10 even numbers are:
0 2 4 6 8 10 12 14 16 18 








Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads