BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to find Sum of Natural Numbers

Last Updated: September 8, 2017 by Chaitanya Singh | Filed Under: Java Examples

The positive integers 1, 2, 3, 4 etc. are known as natural numbers. Here we will see three programs to calculate and display the sum of natural numbers.

  • First Program calculates the sum using while loop
  • Second Program calculates the sum using for loop
  • Third Program takes the value of n(entered by user) and calculates the sum of n natural numbers

To understand these programs you should be familiar with the following concepts of Core Java Tutorial:

  • Java For loop
  • Java While loop

Example 1: Program to find the sum of natural numbers using while loop

public class Demo {

    public static void main(String[] args) {

       int num = 10, count = 1, total = 0;

       while(count <= num)
       {
           total = total + count;
           count++;
       }

       System.out.println("Sum of first 10 natural numbers is: "+total);
    }
}

Output:

Sum of first 10 natural numbers is: 55

Example 2: Program to calculate the sum of natural numbers using for loop

public class Demo {

    public static void main(String[] args) {

       int num = 10, count, total = 0;

       for(count = 1; count <= num; count++){
           total = total + count;
       }

       System.out.println("Sum of first 10 natural numbers is: "+total);
    }
}

Output:

Sum of first 10 natural numbers is: 55

Example 3: Program to find sum of first n (entered by user) natural numbers

import java.util.Scanner;
public class Demo {

    public static void main(String[] args) {

        int num, count, total = 0;

        
        System.out.println("Enter the value of n:");
        //Scanner is used for reading user input
        Scanner scan = new Scanner(System.in);
        //nextInt() method reads integer entered by user
        num = scan.nextInt();
        //closing scanner after use
        scan.close();
        for(count = 1; count <= num; count++){
            total = total + count;
        }

        System.out.println("Sum of first "+num+" natural numbers is: "+total);
    }
}

Output:

Enter the value of n:
20
Sum of first 20 natural numbers is: 210

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Sunny Number Program in Java
  3. Java Program to Add two Binary Numbers
  4. Java Program to reverse the Array
  5. Neon Number in Java with example

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap