Home »
Java Programs »
Core Java Example 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
me@linux:~$ javac Odd.java
me@linux:~$ java Odd
Enter value n : 10
1 3 5 7 9
Core Java Example Programs »