Java Program to Find the Sum of Natural Numbers

Introduction

The sum of natural numbers up to a given number ( n ) is a common mathematical problem. Natural numbers are positive integers starting from 1. The sum of the first ( n ) natural numbers can be calculated using either a loop or the formula ( \text{Sum} = \frac{n \times (n + 1)}{2} ). This guide will walk you through writing a Java program that calculates the sum of the first ( n ) natural numbers.

Problem Statement

Create a Java program that:

  • Prompts the user to enter a positive integer ( n ).
  • Calculates the sum of the first ( n ) natural numbers.
  • Displays the result.

Example:

  • Input: 5

  • Output: "The sum of the first 5 natural numbers is 15"

  • Input: 10

  • Output: "The sum of the first 10 natural numbers is 55"

Solution Steps

  1. Read the Number: Use the Scanner class to take the number ( n ) as input from the user.
  2. Calculate the Sum Using a Loop: Use a loop to sum up all numbers from 1 to ( n ).
  3. Display the Result: Print the calculated sum.

Java Program Using a Loop

// Java Program to Find the Sum of Natural Numbers
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class SumOfNaturalNumbers {
    public static void main(String[] args) {
        // Step 1: Read the number from the user
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter a positive integer: ");
            int n = scanner.nextInt();
            
            // Step 2: Calculate the sum of natural numbers using a loop
            int sum = 0;
            for (int i = 1; i <= n; i++) {
                sum += i;
            }
            
            // Step 3: Display the result
            System.out.println("The sum of the first " + n + " natural numbers is " + sum);
        }
    }
}

Java Program Using the Formula

// Java Program to Find the Sum of Natural Numbers Using Formula
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class SumOfNaturalNumbersFormula {
    public static void main(String[] args) {
        // Step 1: Read the number from the user
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter a positive integer: ");
            int n = scanner.nextInt();
            
            // Step 2: Calculate the sum using the formula
            int sum = n * (n + 1) / 2;
            
            // Step 3: Display the result
            System.out.println("The sum of the first " + n + " natural numbers is " + sum);
        }
    }
}

Explanation

Step 1: Read the Number

  • The Scanner class is used to read an integer input from the user. The nextInt() method captures the input number ( n ).

Step 2: Calculate the Sum

  • Using a Loop: A for loop is used to iterate through numbers from 1 to ( n ), adding each number to a cumulative sum.
  • Using the Formula: The sum of the first ( n ) natural numbers is calculated using the formula ( \text{Sum} = \frac{n \times (n + 1)}{2} ).

Step 3: Display the Result

  • The program prints the calculated sum using System.out.println().

Output Example

Example 1: Using a Loop

Enter a positive integer: 5
The sum of the first 5 natural numbers is 15

Example 2: Using the Formula

Enter a positive integer: 10
The sum of the first 10 natural numbers is 55

Conclusion

These Java programs demonstrate how to calculate the sum of the first ( n ) natural numbers using both a loop and a mathematical formula. They cover essential concepts such as loops, arithmetic operations, and user input handling, making them valuable exercises for beginners learning Java programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top