Java Program to Calculate the Sum of Natural Numbers

Java Program to Calculate the Sum of Natural Numbers

Java Strings

  • In Java, you can convert a String into an InputStream using the ByteArrayInputStream class. The ByteArrayInputStream class is a subclass of InputStream and can be used to read data from a byte array.
  • You must be familiar with following topics to understand the correspond example Such as: Java Strings,
    Java for loops, Java while and do.. while loop.
  • To understand the how to Calculate the sum of Natural Numbers, Read the Complete Article.

Steps to Calculate the Sum of Natural Numbers in Java Program.

  • Here are the Steps to Calculate the Sum of Natural Numbers in Java Program:, You can follow these steps:
    • Define the variable n to represent the number of natural numbers to be summed.
    • Initialize a variable sum to store the sum of the natural numbers.
    • Use a loop to iterate over the natural numbers from 1 to n.
    • In each iteration, add the current number to the sum variable.
    • After the loop has completed, print the value of the sum variable to the console.
while (condition) {
   statement(s);
}
for (initialization; condition; increment) {
   statement(s);
}
  • Initialization: This section is executed only once at the beginning of the loop and is used to initialize the loop counter or index.
  • Condition: This section is evaluated at the beginning of each iteration. If the condition is true, the loop continues to execute. If the condition is false, the loop terminates.
  • Increment: This section is executed at the end of each iteration and is used to update the loop counter or index.
  • Statement(s): This section contains the code that is to be executed repeatedly.

Let’s look at a Java Program to Calculate the Sum of Natural Numbers to perform certain operations.

Example 1: Java Program to Calculate the Sum of Natural Numbers.

Run
public class Main
{
  public static void main (String[]args)
  {
    int n = 10;
    int sum = 0;
    for (int i = 1; i <= n; i++)
      {
	sum += i;
      }
    System.out.println ("Sum of first " + n + " natural numbers is: " + sum);
  }
}

Output

Sum of first 10 natural numbers is: 55

Example 2 :Java Program to Calculate the Sum of Natural Numbers

Run
public class Main {
  public static void main(String[] args) {
    int n = 100;
    int sum = n * (n + 1) / 2;
    System.out.println("Sum of first " + n + " natural numbers is: " + sum);
  }
}

Output

Sum of first 100 natural numbers is: 5050

Example 3: Java Program to Calculate the Sum of Natural Numbers

Run
public class Main
{
  public static void main (String[]args)
  {
    int n = 50;
    int sum = 0;
    int i = 1;
    while (i <= n)
      {
	sum += i;
	i++;
      }
    System.out.println ("Sum of first " + n + " natural numbers is: " + sum);
  }
}

Output

Sum of first 50 natural numbers is: 1275

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription