4 different C++ programs to Print all even numbers from 1 to 100:
In this post, we will learn how to print all even numbers from 1 to 100 in C++
. We will learn different ways to do that. With this program, you will learn how to use loops and how to print numbers in C++.
A number is called even if it is properly divisible by 2, i.e. if we divide the number by 2, the remainder will be 0. For example, 2, 4, 6, 8 etc. are even numbers.
Method 1: By using a for loop:
Let’s try first by using a for
loop. We can use a loop to run a piece of code repeatedly. We need to provide a way to end the loop after it runs for a specific time, or the program will stuck on that loop. Since we want to print the even numbers from 1 to 100, we can use a loop. Let me show you how to do that with a for
loop.
The syntax of the for
loop is:
for(exp1, condition, exp2){
// body
}
- We need to pass three parameters to the loop. All are optional.
- The first one,
exp1
is an expression to execute before the loop runs. - The second one,
condition
is a condition to define when the loop should stop. The loop will stop once it becomesfalse
. - The third parameter,
exp2
is another expression to execute at the end of each iteration of the loop.
For example,
for (int i = 1; i <= 100; i++)
{
}
This loop will run from i = 1
to i = 100
and on each iteration, it increments the value of i
by 1.
- It initialized the integer variable
i
with the first parameter:int i = 1
. - At the end of each iteration, it increases the value of
i
by 1 with the third parameter:i++
. - It will run until the value of
i
is smaller than or equal to 100,i <= 100
.
If you print the value of i
in its body, it will print from 1 to 100. We can use the same loop to print the even numbers between 1 to 100.
Below is the complete program that prints all even numbers from 1 to 100 using a for
loop:
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 100; i++)
{
if (i % 2 == 0)
{
cout << i << endl;
}
}
return 0;
}
Download it on Github
Here,
- We are using a
for
loop that runs fromi = 1
toi = 100
. - Inside the loop, we are checking if the current value of
i
is properly divisible by 2 or not. The modulo operator%
returns the remainder value. The expressioni % 2
will return the remainder of the division of the variablei
by 2 i.e. if the remainder is 0, the value ofi
is an even number.
If you run this program, it will print all the even numbers from 1 to 100 as like below:
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100
Method 2: By using a for loop and without using the modulo operator:
We have another way to solve this problem. We can use the for
loop without any modulo operator. The loop will start from i = 2
and it will end at i = 100
. At the end of each iteration, we will increment the value of the variable i
by 2. So, the value of the variable i
will always be an even value.
#include <iostream>
using namespace std;
int main()
{
for (int i = 2; i <= 100; i = i + 2)
{
cout << i << endl;
}
return 0;
}
Download it on Github
It will print the same output.
It will be faster than the previous example because we are running the loop only for even numbers, not for all numbers.
Method 3: By using a while loop:
Similar to a for
loop, we can use a while
loop as well. The syntax of the while
loop is:
while(condition){
// body
}
It checks one condition
and runs the body of the loop until the condition
is true
. Once this condition becomes false
, it stops. If any variable is used in the condition
, we need to change the value of the variable in the loop body.
Below is the complete program that uses a while
loop to print the even numbers between 1 to 100:
#include <iostream>
using namespace std;
int main()
{
int i = 2;
while (i <= 100)
{
cout << i << endl;
i = i + 2;
}
return 0;
}
Download it on Github
- We have initialized one variable
i
as 2 before the loop starts. - The
while
loop runs till the value ofi
is less than or equal to 100. - Inside the loop, we are printing the value of
i
and we are updating it asi = i+2
.
If you run this program, it will print the same output.
Method 4: By using do…while loop:
The do...while
loop is similar to the while
loop. It executes the body part before it checks the condition. The syntax of the do...while
loop is:
do{
// body
}while(condition);
It will always execute once even if the condition
is false
. Similar to the previous example, we can use it to print even numbers from 1 to 100:
#include <iostream>
using namespace std;
int main()
{
int i = 2;
do
{
cout << i << endl;
i = i + 2;
} while (i <= 100);
return 0;
}
Download it on Github
It will print the same output.
You might also like:
- C++ program to merge two arrays into one array
- C++ example program to move a block of memory
- C++ setw function explanation with example
- How to swap two numbers using pointer in C++
- C++ setfill method example
- C++ program to convert 12 hours to 24 hours time format
- How to find compound interest in C++