Java Program to Calculate the Sum of Odd Numbers in a Given Range

In this article, we will learn how to write a Java program to calculate the sum of all odd numbers within a specified range. The concept is fairly simple: Odd numbers are numbers that cannot be divided evenly by 2 (e.g., 1, 3, 5, 7, 9, ...). Given a range, we can loop through all numbers in the range, check if they are odd, and then add them to a sum variable.

Steps to Calculate the Sum of Odd Numbers

  1. Input the range: Get the start and end values of the range from the user.
  2. Check if a number is odd: If the number is divisible by 2 (i.e., number % 2 != 0), then it is odd.
  3. Sum the odd numbers: If a number is odd, add it to a sum variable.
  4. Print the result: Display the sum of odd numbers.

Example Code

Here’s the Java program that calculates the sum of all odd numbers in a specified range.

import java.util.Scanner;

public class SumOfOddNumbers {
    public static void main(String[] args) {
        
        // Create a Scanner object to take input from the user
        Scanner scanner = new Scanner(System.in);
        
        // Ask the user to enter the start of the range
        System.out.print("Enter the start of the range: ");
        int start = scanner.nextInt();
        
        // Ask the user to enter the end of the range
        System.out.print("Enter the end of the range: ");
        int end = scanner.nextInt();
        
        // Initialize a variable to store the sum of odd numbers
        int sum = 0;
        
        // Loop through the range
        for (int i = start; i <= end; i++) {
            // Check if the number is odd
            if (i % 2 != 0) {
                sum += i; // Add the odd number to sum
            }
        }
        
        // Output the result
        System.out.println("The sum of odd numbers between " + start + " and " + end + " is: " + sum);
        
        // Close the scanner object to prevent memory leaks
        scanner.close();
    }
}

Explanation of the Code

  1. Scanner: We use the Scanner class to take input from the user. The program first asks the user for the start and end values of the range.
  2. Looping through the range: We use a for loop to iterate over all numbers in the range from start to end. For each number, we check if it is odd using the condition i % 2 != 0. If the number is odd, it gets added to the sum.
  3. Condition for Odd Numbers: The condition i % 2 != 0 ensures that we only add the odd numbers to the sum. The modulo operator (%) gives the remainder of a division. If the remainder, when divided by 2 is not 0, the number is odd.
  4. Displaying the Result: After the loop finishes, we print the sum of all odd numbers in the specified range.

Output

Here’s how the sum is calculated.

  • Odd numbers in the range 1 to 10 are: 1, 3, 5, 7, 9.
  • Sum = 1 + 3 + 5 + 7 + 9 = 25.

Conclusion

This Java program demonstrates how to calculate the sum of all odd numbers within a given range. It uses basic Java constructs like loops, conditionals, and input/output functions. This is a great exercise for students to practice their understanding of loops, conditions, and arithmetic operations in Java.

By modifying the program, you can explore different variations, such as finding the sum of even numbers or applying the same concept to other ranges or conditions.


Similar Articles