69.6k views
5 votes
Write an algorithm to print even numbers from 1 to 100

User Suresiva
by
7.8k points

1 Answer

1 vote

Answer:

// Java solution

for ( int i = 0; i < 101; i++)

{

if ( i % 2 == 0)

{

System.out.println( i + " ");

}

}

Using modulus, whatever number we are currently on in our loop is evaluated to see if it is even. Any number divided by 2 that is even will not have a remainder, hence ==0. So we just print the numbers that satisfy that condition and we have all evens.

User Tarun Mathur
by
8.2k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.