Write a program to print all odd number between 1 to 100


  • The Odd_Numbers class is defined and the main method is declared.
  • Inside the main method, a Scanner object input is created to accept input from the user.
  • The program prompts the user to enter the limit up to which the odd numbers should be printed.
  • The program reads the user's input using the nextInt() method of the Scanner object and stores it in an integer variable l.
  • The program enters a for loop that will execute from 1 to the value of l.
  • Inside the for loop, the program checks if the current value of the loop variable s is odd or not by using the modulus operator %. If the remainder of s divided by 2 is equal to 1, then s is an odd number.
  • If s is odd, the program prints the value of s to the console using the println() method of the System.out object.
  • The program then increments the value of s by 1 and returns to the beginning of the for loop.
  • If s is not odd, the program skips the println() statement and returns to the beginning of the for loop.
  • The loop continues to execute until the value of s exceeds the limit specified by the user.

Source Code

import java.util.Scanner;
class Odd_Numbers
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Number of Limit : ");
		int l =input.nextInt();
		for(int s=1;s<=l;s++)
		{
			if(s%2==1)
				System.out.println(s);
		}
	}
}

Output

Enter The Number of Limit : 50
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

Example Programs