Java Program To Print Odd Numbers From 1 To 100

In this program, we will learn to code the Java Program To Print Odd Numbers From 1 To 100. Let’s understand How to print all the Odd numbers from 1 to 100 in Java Programming Language. In previous programs, we have learned to code the Java Program to Check Even or Odd Numbers.

Let’s see the code of the Java Program To Print Odd Numbers From 1 To 100.

Java Program To Print Odd Numbers From 1 To 100

import java.util.*;

public class Main {

  public static void main(String[] args) {
    
    System.out.println("Odd Numbers from 1 to 100 are :");
    for(int num=1 ; num <= 100 ; num++){
      if(num % 2 != 0){
        System.out.print(""+num+" ");
      }
    }

  }
}

Output

Odd Numbers from 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 ?

    for(int num=1 ; num <= 100 ; num++){
      if(num % 2 != 0){
        System.out.print(""+num+" ");
      }
    }

In this program, we have a loop statement from 1 to 100 which checks for every value of i.

If i % 2 != 0 is true, then that value of i is an odd number and we will print it.

This loop continues until i becomes 100.

Java Program To Print Odd Numbers From 1 TO N

import java.util.*;

public class Main {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter maximum range: ");
    int N = sc.nextInt();
    
    System.out.println("Odd Numbers from 1 to "+N+" are :");
    for(int num=1 ; num <= N ; num++){
      if(num % 2 != 0){
        System.out.print(""+num+" ");
      }
    }

  }
}

Output

Enter maximum range: 
Odd Numbers from 1 to 21 are :
1 3 5 7 9 11 13 15 17 19 21 

Java Program To Print Odd Numbers from 1 To N Without Using If Statement

import java.util.*;

public class Main {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter maximum range: ");
    int N = sc.nextInt();
    
    System.out.println("Odd Numbers from 1 to "+N+" are :");
    for(int num=1 ; num <= N ; num+=2){
      
        System.out.print(""+num+" ");
      
    }

  }
}

Output

Enter maximum range: 
Odd Numbers from 1 to 51 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 

Conclusion

I hope after going through this post, you understand Java Program To Print Odd Numbers From 1 To 100. We discussed two approaches in this post. We also learned to code the Java Program To Print Odd Numbers From 1 TO N.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *