In java, to print all odd numbers in a given range we can use loop concepts such as while loop or for loop. In this programming tutorial we will learn writing Java program that will print all the odd numbers in given range. If the input range is between the 1 and 100, it should print 1,3,5,7….99 as the output.
In this article, we will also see the examples, detailed logic and program explanation for the better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We need to write a Java program to print all odd numbers in a given range. The program should take two input values as first number and last number as a range and then it should print odd numbers in that range as output. This action should be done in our program. Let’s see an example
Example 1:
Input: enter the first number
1
enter the last number
100
Output: 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
Here for the input range 1 to 100, Output is all the odd numbers between those range.
Example 2:
Input: enter the first number
4
enter the last number
10
Output: 5 7 9
Here for the input range 4 to 10, Output is all the odd numbers between those range.
Logic to print all Odd numbers in a given range
To find the odd numbers within a given range, we first iterate over each number in the range. For each number, we check if it is divisible by 2 or not using the modulo operator (%). Specifically, we use the expression n%2. If the result is 0, the number n is even so will skip and print the number which is not divisible by 2 which is odd number.
Java program to print all Odd numbers in a given range
Program 1: Using while loop
In the below program we are using while loop to print all odd numbers in a given range in Java.
import java.util.Scanner;
public class OddNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the first number");
int firstNumber = s.nextInt();
System.out.println("enter the last number");
int lastNumber = s.nextInt();
int value = firstNumber;
while (value <= lastNumber) {
if (value % 2 != 0)
System.out.println(value);
value++;
}
s.close();
}
}
Output:
enter the first number
1
enter the last 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 explanation to print all the odd numbers in a given range
- The java program asks the user to input two integers:
firstNumber
andlastNumber
. - These represent the starting and ending numbers of the range to be checked for odd numbers.
- It uses a
while
loop to iterate through each number fromfirstNumber
tolastNumber
, inclusive. - Within the loop, it checks if the current number (
value
) is odd using the conditionvalue % 2 != 0
. - If the condition is true, it prints the current number (
value
), indicating it’s an odd number. - After each iteration, the value of
value
is incremented by 1 (value++
). - There is no input validation to ensure
firstNumber
is less than or equal tolastNumber
, assuming valid inputs from the user. - Finally, it closes the
Scanner
objects
to release system resources.
Program 2: Using for loop
In the below program we are using for loop to print all odd numbers in a given range in Java.
import java.util.Scanner;
public class OddNumbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number");
int firstNumber=s.nextInt();
System.out.println("Enter the last number");
int lastNumber=s.nextInt();
for(int i= firstNumber; i<=lastNumber;i++) {
if(i%2!=0)
System.out.println(i);
}
s.close();
}
}
Output:
Enter the first number
2
Enter the last number
8
3
5
7
Java program explanation to print all the odd numbers in a given range
- The java program asks the user to input two integers:
firstNumber
andlastNumber
. - These represent the starting and ending numbers of the range to be checked for odd numbers.
- It uses a
for
loop to iterate through each number fromfirstNumber
tolastNumber
, inclusive. - Within the loop, it checks if the current number (
i
) is odd using the conditioni % 2 != 0
. - If the condition is true, it prints the current number (
i
), indicating it’s an odd number. - There is no input validation to ensure
firstNumber
is less than or equal tolastNumber
, assuming valid inputs from the user. - Finally, it closes the
Scanner
objects
to release system resources.
Conclusion
In the above programming tutorial we have learnt printing the all odd numbers in a given range using while loop and for loop. Hope this tutorial helped you in understanding the program.
You can also check our awesome programming tutorials
- Java Program to count the digits of a given number.
- Java Program to print the sum of digits of a given number.
- Java Program to print all Prime numbers between in a given range.
- Java Program to find sum of all prime numbers in a given range.
- Java Program to check a given number is Armstrong or not.
Happy Coding !!