C++ Program to Print Odd Numbers From 1 to 100

Howdy readers, today you will learn how to write a C++ program to print odd numbers from 1 to 100.

In this tutorial, we print all the odd numbers lying between 1 to 100 using the following methods:

  1. Using For Loop
  2. Using While Loop
  3. Without Using If Statement

So, without any delay, let’s begin this tutorial.

C++ Program to Print Odd Numbers From 1 to 100 Using For Loop

C++ Program

// C++ Program to Print Odd Numbers From 1 to N Using For Loop
#include <iostream>
using namespace std;

int main(){
    int i;
    
    // Odd Numbers from 1 to 100
    for (i = 1; i <= 100; i++){
        if (i % 2 != 0){
            cout << i << " ";
        }
    }
    
    return 0;
}

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 
C++ Program to Print Odd Numbers From 1 to 100 Using For Loop

Explanation

    for (i = 1; i <= 100; i++){
        if (i % 2 != 0){
            cout << i << " ";
        }
    }

In this program, we iterate the numbers from 1 to 100. In each iteration, we check whether the value of i is not divisible by 2 or not.

If i is not divisible by 2, we print that iteration of i. After that, we increase the value of i by 1. This process keeps on executing until i <= 100.

This gives us all the odd numbers lying between 1 to 100.

C++ Program to Print Odd Numbers From 1 to 100 Using While Loop

C++ Program

// C++ Program to Print Odd Numbers From 1 to 100 Using While Loop
#include <iostream>
using namespace std;

int main(){
    int i = 1;
    
    // Odd numbers using while loop
    while (i <= 100){
        if (i % 2 != 0){
            cout << i << " ";
        }
        i++;
    }
    
    return 0;
}

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 
C++ Program to Print Odd Numbers From 1 to 100 Using While Loop

C++ Program to Print Odd Numbers From 1 to 100 Without Using If Statement

C++ Program

// C++ Program to Print Odd Numbers From 1 to 100 Without Using If Statement
#include <iostream>
using namespace std;

int main(){
    int i = 1;
    
    // Without using the if statement
    while (i <= 100){
        cout << i << " ";
        i = i + 2;
    }
    
    return 0;
}

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 
C++ Program to Print Odd Numbers From 1 to 100 Without Using If Statement

Explanation

    int i = 1;

We have declared an int data type variable named i. This variable has been assigned a value of 1.

    while (i <= 100){
        cout << i << " ";
        i = i + 2;
    }

Now, we use a while loop to iterate numbers from 1 to 100. Since the loop starts with i = 1, we print 1st iteration: i = 1.

After that, the value of i is increased by 2. This gives us i = i + 2 = 1 + 2 = 3.

Therefore,

  • 2nd Iteration = i + 2 = 1 + 2 = 3.
  • 3rd Iteration = i + 2 = 3 + 2 = 5.
  • 4th Iteration = i + 2 = 5 + 2 = 7.
  • . . . 
  • . . . and so on.

This loop keeps on executing until i <= 100.

Conclusion

Today you learned how to write a C++ program to print odd numbers from 1 to 100.

If you have any doubts regarding the tutorial, comment down your questions in the comment section.

Thanks for reading.

Happy Coding!!

Also Read:

Leave a Comment