Java Program to print sum of odd numbers between 1 to n using while loop

Java Program to print all odd number between 1 to 100 using while loop.

Any number when divided by 2 gives a remainder other than zero which is an odd number. Here we are writing a Java program using a while loop to print all the odd numbers between 1 and 100.

Steps to write a java program:

  1. In the below Java Program, we are starting iteration with 1 and checking its divisibility by 2 using the if statement.
  2. If the condition is satisfied, we print it as the odd number.
  3. For eg . 3%2 = 1, which means 3 is odd number and it meets our if condition. So we are printing the number as odd and then increasing the value by 1.

Java Program to print Odd numbers between 1 to 100 using while loop

import java.util.*;
public class Main
{
	public static void main(String args[])
	{
		int start = 1;
		int end = 100; 
		System.out.println("Odd numbers between "+ start+ " and " +end+ " are :");
		while(start<=end) {
			if(start%2 != 0) {
			System.out.println(start);
			}
			start++;
		}
	}
}

Output

Odd numbers between 1 and 100 are :
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