Java Program to Print Odd Numbers From 1 to N

You are currently viewing Java Program to Print Odd Numbers From 1 to N

Java program to print ODD numbers from 1 to N

//Java program to print ODD numbers from 1 to N.
 
import java.util.*;
 
public class Odd{
 
    public static void main(String []args)
    {
        int n=0,i=0;
         
        Scanner X = new Scanner(System.in);
         
        System.out.print("Enter value n : ");
        n = X.nextInt();
         
        for(i=1; i<n; i++)
        {
            if(i%2!=0)
                System.out.print(i+" ");
        }   
        System.out.println();
         
    }
}

Output

    
    [email protected]:~$ javac Odd.java
    [email protected]:~$ java Odd

    Enter value n : 10
    1 3 5 7 9

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply