How to find and list odd numbers between 1 and any given number. - Java Language Basics

Java examples for Language Basics:int

Description

How to find and list odd numbers between 1 and any given number.

Demo Code

 
public class Main {
        public static void main(String[] args) {
                int limit = 50;
                 System.out.println("Printing Odd numbers between 1 and " + limit);
                 for(int i=1; i <= limit; i++){
                        //if the number is not divisible by 2 then it is odd
                        if( i % 2 != 0){
                                System.out.print(i + " ");
                        }/* w w  w .ja  va  2s. c  o  m*/
                }
        }
}

Result


Related Tutorials