ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » Java solved programs » Core java programs

Java program to print ODD numbers from 1 to N

Print Odd Numbers from 1 to N in Java - In this program we will read value of N and print Odd numbers from 1 to N.

Print Odd Numbers from 1 to N using Java Program

//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

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.