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.
Java while and do...while Loop :
In Java, there are two other types of loops besides the for loop: the while loop and the do...while loop. These loops are used to repeat a block of code an unknown number of times until a certain condition is met.
The while loop has the following syntax:
while (condition) { statement(s); }
Java for Loop
A for loop in Java is a control structure used to repeat a block of code a specified number of times. It is commonly used for iterating through arrays or other collections of data.The basic syntax of a for loop in Java is:
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
Explanation:
This program uses a for loop to calculate the sum of the first n natural numbers.
Beginning with 1, the loop iterates n times, adding each integer to the sum variable.
The outcome is then printed to the console.
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
Explanation:
In this code, the formula n * (n + 1) / 2 is used to calculate the sum of the first n natural numbers. The result is then printed to the console.
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
Explanation:
The total variable is initialised to 0 and the I variable is initialised to 1 in this line of code.
While I is less than or equal to n, the while loop keeps going. The sum variable is increased by the value of I each iteration, and I is also increased by 1. The outcome is then printed to the console.
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
Login/Signup to comment