# Java Program to Print Odd Numbers from 1 to N

How to write a Java Program to Print Odd Numbers from 1 to N using For Loop, While Loop with example. If the given number is not divisible by 2, it is an odd number.

## Java Program to Print Odd Numbers from 1 to N Example 1

This Java program allows entering the maximum limit value. Next, this Java program prints the odd numbers from 1 to maximum limit value using For Loop and If statement.

In Java, we have a % Arithmetic Operator to check the remainder. If the remainder is not 0, the number is odd.

```// Java Program to Print Odd Numbers from 1 to N using For loop
import java.util.Scanner;

public class OddNumbers1 {
private static Scanner sc;
public static void main(String[] args)
{
int number, i;
sc = new Scanner(System.in);

System.out.print(" Please Enter any Number : ");
number = sc.nextInt();

for(i = 1; i <= number; i++)
{
if(i % 2 != 0)
{
System.out.print(i +"\t");
}
}
}
}```

The for loop iterates from 1 to maximum value (Here, number = 5). And we know, if the number is not divisible by 2, it is an odd number. So, we used the Java If condition to check the remainder.

User entered value: number  = 5

For Loop First Iteration: for(i = 1; i <= 5; i++)
if(i % 2 != 0) => if(1 % 2 != 0)
Condition is True. So, i value printed

Second Iteration: for(i = 2; 2 <= 5; 2++)
if(2 % 2 != 0) – Condition is False.

Third Iteration: for(i = 3; 3 <= 5; 3++)
if(3 % 2 != 0) – the For Loop Condition is True. So, i value printed

Fourth Iteration: for(i = 4; 4 <= 5; 4++)
if(4 % 2 != 0) – Condition is False.

Fifth Iteration: for(i = 5; 5 <= 5; 5++)
if(5 % 2 != 0) – Condition is True. So, it prints i

Sixth Iteration: for(i = 6; 6 <= 5; 6++)
Condition (6 <= 5) is False. So, the Java compiler exits from the For Loop

## Java Program to Print Odd Numbers from 1 to N Example 2

This Java program to display odd numbers from 1 to N is the same as above, but we altered the for loop to eliminate the If statement. If you observe the Java code, we started i from 1 and incremented by 2 (not 1). It means for the first iteration i = 1, and for the second iteration i = 3 (not 2) so on.

```import java.util.Scanner;

public class OddNumbers2 {
private static Scanner sc;
public static void main(String[] args)
{
int number, i;
sc = new Scanner(System.in);

System.out.print(" Please Enter any Number : ");
number = sc.nextInt();

for(i = 1; i <= number; i = i + 2)
{
System.out.print(i +"\t");
}
}
}```
``````Please Enter any Number : 20
1	3	5	7	9	11	13	15	17	19	``````

## Java Program to Print Odd Numbers from 1 to N Example 3

This Java program to return odd numbers from 1 to 100 is the same as the second example, but we used While Loop.

```// Java Program to Print Odd Numbers from 1 to N using while loop
import java.util.Scanner;

public class OddNumbers3 {
private static Scanner sc;
public static void main(String[] args)
{
int number, i;
sc = new Scanner(System.in);

System.out.print(" Please Enter any Number : ");
number = sc.nextInt();
i = 1;

while(i <= number)
{
System.out.print(i +"\t");
i = i + 2;
}
}
}```

Odd numbers from 1 to 30 using a while loop output

``````Please Enter any Number : 30
1	3	5	7	9	11	13	15	17	19	21	23	25	27	29	``````

## Java Program to Print Odd Numbers from 1 to N using Method

The Java program of this odd number is the same as the first example. Still, we separated the logic and placed it in a separate method.

```import java.util.Scanner;

public class OddNumbers4 {
private static Scanner sc;
public static void main(String[] args)
{
int number;
sc = new Scanner(System.in);

System.out.print(" Please Enter any Number : ");
number = sc.nextInt();

findOddNum(number);
}

public static void findOddNum(int num)
{
int i;
for(i = 1; i <= num; i++)
{
if(i % 2 != 0)
{
System.out.print(i +"\t");
}
}
}
}```

Odd numbers from 1 to 100 using functions output

``````Please Enter any Number : 100
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	``````

## Java Program to Print Odd Numbers between a Given Range

This Java program allows the user to enter Minimum and maximum value. Next, the Java example returns odd numbers between Minimum value and maximum value.

```import java.util.Scanner;

public class Example5 {

private static Scanner sc;
public static void main(String[] args)
{
int minimum, maximum;
sc = new Scanner(System.in);

System.out.print(" Please Enter the Minimum value : ");
minimum = sc.nextInt();

System.out.print(" Please Enter the Maximum value : ");
maximum = sc.nextInt();

findOddNum(minimum, maximum);
}

public static void findOddNum(int minimum, int maximum)
{
int i;
if(minimum % 2 == 0)
{
minimum++;
}
for(i = minimum; i <= maximum; i++)
{
if(i % 2 != 0)
{
System.out.print(i +"\t");
}
}
}
}```

Odd numbers from 1 to N using for loop output

``````Please Enter the Minimum value : 5
Please Enter the Maximum value : 60
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	``````