VOOZH about

URL: https://www.geeksforgeeks.org/computer-science-fundamentals/program-to-print-first-10-even-numbers/

⇱ Program to print first 10 even numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to print first 10 even numbers

Last Updated : 29 Jan, 2024

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

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.

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

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

Comment
Article Tags: