Java Program to Calculate the Sum of Natural Numbers

In this tutorial you will learn about the Java Program to Calculate the Sum of Natural Numbers and its application with practical example.

In this tutorial, we will learn to create a Java program that will Calculate the sum of Natural Number using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output
  • Class and Object.
  • Basic Java programming.
  • Loop in Java 
  • While loop For loops.

What Is Natural  Number?

Simply all the counting numbers  {1, 2,3.4…} are commonly called Natural numbers.Natural numbers  include all the positive integers from 1 to infinity.

Java Program to Calculate the Sum of Natural Numbers

In this program we will Create a Java program that will Calculate the sum of Natural Number.Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will find the sum of all natural number to given number.

Output


Our  program loops from 1 to the given (10) and adds all numbers to the variable Sum
sum=sum+i. and after calculating value we will print sum or all natural number.

In the above program, we have first declared and initialized a set variables required in the program.

  • number=to take value from user.
  • i =For iteration.
  • sum = for counting sum of numbers.

I

In the above program, we have first declared and initialized a set variables required in the program. In our  program first we will take a value from user.After that we will start loop from 1 to given number.

Within the loop, we add all values in variable Sum .After calculating values  of all natural number between the given numbers natural number.

At last we will Print sum of the all Natural number between 1 to given number.

In this tutorial we have learn about the Java Program to Calculate the Sum of Natural Numbers and its application with practical example. I hope you will like this tutorial.