C++ program to print all odd numbers from 1 to 100

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.

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.

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 for loop, while loop and do while loop in C++
  • How to print values in C++
  • How to check the remainder using modulo operator.

Modulo operator:

Modulo operator is defined by % symbol. By using 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.

So, 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 concept to print all odd numbers from 1 to 100.

C++ program to print all odd numbers from 1 to 100 using a for loop:

Let’s use a for loop to print all odd numbers from 1 to 100 by using a for loop. for loop is defined as like below:

for(start_condition, termination_condition, end_condition){
    //body
}

Here,

  • start_condition is a condition that runs at the start of the loop. This is used to initialize a variable, that is used in the loop.
  • termination_condition is a condition that is used to define when to terminate the loop. The loop will exit when it returns false.
  • 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;
}

Here,

  • It uses a for loop that runs from i = 1 to i = 100, i.e. it is iterating for all numbers from 1 to 100.
  • For each value of i, it is checking if it is odd or not.
  • The if statement is checking if i is odd or not. It uses modulo operator i%2 to check the remainder, i.e. if it is 1 or not.
  • If it is odd, it prints the i value.

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 

C++ program to print all odd numbers from 1 to 100 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 while loop is:

while(condition){
    //body
}

The body will run until the condition is true. Once the condition become 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;
}

It is almost similar to the above program. We are using i to run the loop. The loop runs until the value of i is less than or equal to 100.

  • Inside the loop, we are checking if the value of i is odd or not and printing that value.
  • At the end of the loop, it is incrementing the value of i by 1. It makes sure that the loop will exit when the value of i is 100.

If you run this app, it will print similar output as the above 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 

Using a do-while loop:

We can also use a do-while loop. It is almost similar to a while loop.

do{
    //body
}while(condition);

It will keep running the code defined in the body until the value of condition is true. Once it become false, it will stop it.

Let’s write it 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;
}

It will print similar output as the above programs.

Optimized way to do this:

Actually, we don’t have to check for each number if it is divisible by 2 or not. We can start from 1 and increment by 2 each time.

Below program uses this way for all of these three methods we discussed:

#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;
}

This process will be faster as we are jumping two 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

C++ print odd numbers 1 to 100

You might also like: