In this post, we will learn how to print odd numbers from 1 to 100 using C++ Programming language.
Any number which is not exactly divisible by 2 is called odd numbers. For example: 3, 12, 27, and so on.
We will find odd numbers by checking whether the numbers are divisible by 2 or not. We will perform this task using following methods:
- Using For Loop
- Using While Loop
- Using Do While Loop
So, without further ado, let’s begin this tutorial.
C++ Program to Print Odd Numbers From 1 To 100 Using For Loop
// C++ Program To Print Odd Numbers From 1 To 100 #include <iostream> using namespace std; int main(){ int i; cout << "Odd numbers between 1 to 100 are: " << endl; for (i = 1; i <= 100; i++){ if (i % 2 != 0) cout << i << " "; } return 0; }
Output
Odd numbers between 1 to 100 are:
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
How Does This Program Work ?
int i;
In this program, we have defined a variable named i.
for (i = 1; i <= 100; i++){ if (i % 2 != 0) cout << i << " "; }
Then, we find all the odd numbers less than 100 using for loop statement. And the odd numbers are displayed on the screen using cout statement.
C++ Program to Print Odd Numbers From 1 To 100 Using While Loop
// C++ Program to Print Odd Numbers From 1 To 100 Using While Loop #include <iostream> using namespace std; int main(){ int i; i = 1; cout << "Odd Numbers between 1 to 100 are: \n"; while (i <= 100){ cout << i << " "; i = i + 2; } return 0; }
Output
Odd Numbers between 1 to 100 are:
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
C++ Program to Print Odd Numbers From 1 to 100 Using Do While Loop
// C++ Program to Print Odd Numbers From 1 to 100 Using Do While Loop #include <iostream> using namespace std; int main(){ int i = 1; cout << "Odd numbers between 1 to 100 are: " << endl; do{ if (i % 2 == 1){ cout << i << " "; } i++; } while (i <= 100); return 0; }
Output
Odd numbers between 1 to 100 are:
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
Conclusion
I hope after going through this post, you understand how to print odd numbers from 1 to 100 using C++ Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to guide you.
Also Read: