In java, to print sum of all odd numbers in a given range we can use loop concepts such as while loop or for loop. For a given range, we should print the sum of all odd numbers.
In this article, we will learn writing the Java Program to calculate the sum of all odd numbers in the given range and print them as an output. we will learn this program through examples, logics and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We have to write a Java program to print sum of all odd numbers in a given range. Our program should take two input values as first number and last number as a range and then it should print sum of odd numbers in that range as output. Here input numbers are inclusive in the result calculation. This action should be done in our program.
Let’s see an example,
Example
For a given input range 1 to 5, Output should be 9
Input first number: 1
Input last number: 5
Odd numbers between 1-5: 1 3 5
Sum: 1+3+5 = 9
For a given range (1,5), the sum of odd numbers is 9.
Logic to print sum of all odd numbers in a given range
To find the sum of 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 using the modulo operator (%). Specifically, we use the expression n%2. If the result is 0, the number n is even and we will skip it as we need odd numbers only. So here else condition will called and we will write old sum logic in else condition. Finally, we print the sum of all the odd numbers as an output.
Java program to print sum of all odd numbers in a given range
Program 1: Using while loop
In the below program we are using while loop to print sum of all odd numbers in a given range.
import java.util.Scanner;
public class SumOfOddNum {
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;
int sum=0;
while(value<=lastNumber) {
if(value%2!=0)
sum += value;
value++;
}
System.out.println("sum: "+sum);
s.close();
}
}
Output:
enter the first number
3
enter the last number
5
sum: 8
Java program explanation to print sum of all the odd numbers in a given range
- Using a Scanner, the program asks the user to input two numbers, the first and last numbers of a range.
- It initializes two variables: one to hold the current number being checked (
value
, it holds first number at the beginning) and another to accumulate the sum of odd numbers (sum
), initially set to zero. - Inside a loop, it checks each number within the range. If a number is odd, it adds it to the sum.
- The loop iterates through numbers from the first to the last number inputted by the user.
- After the loop, it prints the sum of all the odd numbers within the specified range.
- Finally, it closes the Scanner object to free up system resources.
Program 2: Using for loop
In the below program we are using while loop to print all odd numbers in a given range.
import java.util.Scanner;
public class SumOddNumbers {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number");
int number=s.nextInt();
System.out.println("Enter the last number");
int lastNumber=s.nextInt();
int sum=0;
for(int i =number; i<=lastNumber;i++) {
if(i%2!=0)
sum +=i;
}
System.out.println("sum: "+sum);
s.close();
}
}
Output:
Enter the first number
3
Enter the last number
6
sum: 8
Java program explanation to print sum of all the odd numbers in a given range
- Program utilizes a Scanner to ask the user for two integer inputs: the first and last numbers of a range.
- Initializes variables to store the first number (
number
), the last number (lastNumber
), and the sum of odd numbers (sum
), initially set to zero. - Uses a for loop to iterate through numbers from
number
tolastNumber
. If a number is odd (i.e., not divisible by 2), it adds it to the sum. - Prints the sum of all the odd numbers within the specified range.
- Closes the Scanner object (
s
) to release system resources after input is processed.
Conclusion
In this tutorial we have learnt writing the Java program to find the sum of odd numbers in a given range using while loop and for loop. Hope this tutorial helped you in understanding it.
You can also check our another programming tutorials
- Java Program to check whether a given number is Prime or not.
- 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 Perfect or not.
- Java Program to check all Perfect numbers in a given range.
Happy Coding !!