Java program to print odd and even numbers between 1 and 100

Let’s learn java program to print odd and even numbers between 1 and 100.

Java program to print odd and even numbers between 1 and 100

In the below program to print odd and even numbers between 1 and 100 we are using two for loops. One to print even numbers and another to print odd numbers.

public class EvenOddBetween1And100
{
   public static void main(String[] args)
   {
      System.out.println("Even numbers between 1 and 100: ");
      for(int a = 1; a <= 100; a++)
      {
         if(a % 2 == 0)
         {
            System.out.print(a + " ");
         }
      }
      System.out.println("\nOdd numbers between 1 and 100: ");
      for(int a = 1; a <= 100; a++)
      {
         if(a % 2 != 0)
         {
            System.out.print(a + " ");
         }
      }
   }
}

Output:

Even numbers between 1 and 100:
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
Odd numbers between 1 and 100:
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 nested-if statement

In the below java program we have defined a method that hold logic to check odd or even number which uses nested-if statement.

public class OddEvenUsingNestedIf
{
   public static void main(String[] args)
   {
      System.out.println("Odd numbers: ");       
      printOddNumbers(1, 100);
      System.out.println("\n");
      System.out.println("Even numbers: ");       
      printEvenNumbers(1, 100);
   }
   private static void printOddNumbers(int odd, int end)   
   { 
      if(odd > end)   
         return;
      if(odd % 2 != 0)   
      { 
         System.out.print(odd + " ");   
         printOddNumbers(odd + 2, end);   
      }
      else
      { 
         printOddNumbers(odd + 1, end);   
      }
   }
   private static void printEvenNumbers(int even, int end)   
   { 
      if(even > end)   
         return;   
      if(even % 2 == 0)   
      {
         System.out.print(even + " ");   
         printEvenNumbers(even + 2, end);   
      }
      else   
      { 
         printEvenNumbers(even + 1, end);
      }
   }
}

Output:

Odd numbers:
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

Even numbers:
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


Using while loop

Similarly let’s learn to print odd numbers between 1 and 100 using while loop.

import java.util.Scanner;
public class OddEvenUsingWhileLoop
{
   public static void main(String[] args)
   {
      int oddNumber, a;
      Scanner sc = new Scanner(System.in);  
      System.out.print("Please enter limit to print odd numbers: ");  
      oddNumber = sc.nextInt();
      a = 1;
      System.out.print("Odd numbers: ");  
      while(a <= oddNumber)  
      {  
         System.out.print(a + " ");   
         a = a + 2;  
      }
      sc.close();
   }
}

Output:

Please enter limit to print odd numbers: 100
Odd numbers: 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


Now let’s see to print even numbers between 1 and 100 using while loop.

import java.util.Scanner;
public class OddEvenUsingWhileLoop
{
   public static void main(String[] args)
   {
      int evenNumber, a;
      Scanner sc = new Scanner(System.in);  
      System.out.print("Please enter limit to print even numbers: ");  
      evenNumber = sc.nextInt();
      a = 2;   
      System.out.print("Even numbers: ");  
      while(a <= evenNumber)  
      {
         System.out.print(a + " ");   
         a = a + 2;
      }
      sc.close();
   }
}

Output:

Please enter limit to print even numbers: 100
Even numbers: 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