C++ program to print all odd numbers from 1 to 100:
In this post, we will learn how to print all odd numbers from 1 to 100 in C++
. A number is called an odd number if it is not perfectly divisible by 2. Or, if you divide a number by 2, if the remainder is not zero, it is called an odd number.
For example, 11 is an odd number, but 12 is not. If we divide 11 by 2, the remainder is 1. So, it is an odd number. Numbers that are not odd are called even numbers. In this post, we will learn how to print all odd numbers from 1 to 100 in C++
. You can change this program to print odd numbers in any range.
I will show you different ways to solve this problem. You will learn:
- How to use a
for
loop,while
loop anddo...while
loop inC++
- How to print values in C++
- How to check the remainder using the modulo operator.
Modulo operator:
The modulo operator is defined by the %
symbol. With this operator, we can find the remainder of a division operation. For example, if x
and y
are two numbers, x%y
will give the remainder value if x
is divided by y
i.e. it will give the remainder of x/y
.
So, the result of 10%2
is 0
and 11%3
is 2
. If a number x
is odd, the result of x%2
is 1
always. We can use this process to print the odd numbers from 1 to 100.
Method 1: C++ program to print all odd numbers from 1 to 100 by using a for
loop:
Let’s use a for
loop to print all odd numbers from 1 to 100. The syntax of the for loop is:
for(start_condition, termination_condition, end_condition){
//body
}
Here,
- The first parameter
start_condition
is a condition that runs at the start of the loop. This is used to initialize a variable. - The second parameter
termination_condition
is a condition that is used to define when to terminate the loop. The loop will run until the condition istrue
. - The third parameter
end_condition
runs at the end of each iteration. This is used to modify the variable used to run the loop.
Let’s write down the program:
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 100; i++)
{
if (i % 2 == 1)
{
cout << i << " ";
}
}
return 0;
}
Download the program on Github
Here,
- It uses a
for
loop that runs fromi = 1
toi = 100
, i.e. it is iterating for all numbers from 1 to 100. - For each value of
i
, the program checks if the value ofi
is odd or not. - The
if
statement is checking if the value ofi
is odd or not. It uses the modulo operator,i%2
to get the remainder ifi
is divided by 2. It checks if the remainder is 1. If it is 1, the value ofi
will be odd. - If the value is odd, it prints the value of
i
.
If you run the above program, it will print the below output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Method 2: C++ program to print all odd numbers from 1 to 100 by using a while
loop:
We can also use a while
loop to print all odd numbers from 1 to 100. It is almost similar to the above program. The only difference is that we will use a while
loop instead of a for
loop:
The syntax of the while
loop is:
while(condition){
//body
}
The body will run until the condition
is true
. Once the condition
becomes false
, the loop will stop.
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 100)
{
if (i % 2 == 1)
{
cout << i << " ";
}
i++;
}
return 0;
}
Download the program on Github
- We are using the variable
i
to run the loop. The loop runs until the value ofi
is less than or equal to 100. - Inside the loop, we are checking if the value of
i
is odd or not by using the modulo operator and it prints the value ofi
if it is odd. - At the end of the loop, it is increasing the value of
i
by 1. The loop will exit once the value ofi
is 100.
If you run this app, it will print similar output as the previous example.
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Method 3: C++ program to print the odd numbers by using a do...while
loop:
We can also use a do...while
loop. It is almost similar to a while
loop. The syntax of the do...while
loop is:
do{
//body
}while(condition);
It will keep running the code defined in the body until the value of condition
is true
. Once it becomes false
, it will stop.
Let’s write the program using a do...while
loop:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
do
{
if (i % 2 == 1)
{
cout << i << " ";
}
i++;
} while (i <= 100);
return 0;
}
Download the program on Github
It will print similar output as the above programs.
Method 4: Optimized way to print the odd numbers smaller than 100:
We don’t have to check for each number if it is divisible by 2 or not. We can start the loop from 1 and increment the loop variable by 2 on each step. Since 1 is an odd number, if we increment the variable by 2, the variable will always point to an odd number. If the loop starts from 2 and increments it by 2 on each step, it will always point to an even number.
The following program shows how it works with a for
loop, while
loop and do...while
loop:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
cout << "Using a for loop: " << endl;
for (i = 1; i <= 100; i += 2)
{
cout << i << " ";
}
cout << "\n\nUsing a while loop: " << endl;
i = 1;
while (i <= 100)
{
cout << i << " ";
i += 2;
}
cout << "\n\nUsing a do while loop: " << endl;
i = 1;
do
{
cout << i << " ";
i += 2;
} while (i <= 100);
return 0;
}
Download the program on Github
This process will be faster as we are jumping between the odd numbers on each iteration.
It will print:
Using a for loop:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Using a while loop:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Using a do while loop:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
You might also like:
- C++ program to convert 12 hours to 24 hours time format
- How to find compound interest in C++
- C++ program to Print all even numbers from 1 to 100
- How to find the cube of a number using Macros in C++
- C++ program to find the square root of a number
- std::reverse() method in C++ explanation with example
- How to use ternary operator in C++
- Implement bubble sort in C++