Java Program to Find Sum of Natural Numbers17 Mar 2025 | 4 min read The natural numbers are the numbers that include all the positive integers from 1 to infinity. For example, 1, 2, 3, 4, 5, ......, n. When we add these numbers together, we get the sum of natural numbers. In this section, we will create the following programs:
We can also find the sum of n natural number using the mathematical formula: Sum of n natural numbers=n*(n+1)/2 Suppose, we want to find the sum of the first 100 natural numbers. By putting the value in the above formula, we get: In this section, we are going to use the following ways to find the sum of natural numbers.
Using Java for LoopUsing Java for loop is the easiest way to find the sum of natural numbers. SumOfNaturalNumber1.java Output: Sum of First 10 Natural Numbers is = 55 Using Java while LoopIn the following example, we have replaced the for loop with the while loop. The while loop executes until the condition i <= num do not become false. It calculates the sum of natural numbers up to a specified limit. SumOfNaturalNumber2.java Output: Sum of First 100 Natural Numbers is = 5050 Sum of n Natural NumbersThe following program finds the sum of n natural numbers. In this program, we have used the same while loop, as we have used in the above program. We have also taken two inputs from the user i.e. i and num. The variable i is the starting number and the variable num is the end number. For example, if we want to find the sum of natural numbers from 20 (i) to 100 (num). SumOfNaturalNumber3.java Output: Sum from: 1 Sum up to: 1000 Sum of Natural Numbers = 500500 Using FunctionIn the following program, we have found the sum of n natural number using the function. SumOfNaturalNumber4.java Output: Sum of Natural Numbers is: 1275 Let's see another program. In the following program, we have used the formula to find the sum of natural numbers. SumOfNaturalNumber5.java Output: Sum of Natural Number is: 20100 Next TopicJava Tutorial |
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, HCL, IBM, and Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are...
10 min read
In this section, we will write the Java programs to check if a number is positive or negative. We have used the following ways to check the number is positive, negative, or zero. Using Relational Operator Using Math.signum() Method Using Integer.signum() Method Using Bit Shift Operator Using ArrayList class Using Relational Operator To...
5 min read
In this program, we will find out the smallest node in the given binary tree. We first define variable min that will hold root's data. Then, we traverse through left sub-tree to find the smallest node in left subtree. Compare it with min and store...
5 min read
In this program, we need to multiply two matrices and print the resulting matrix. Product of two matrices The product of two matrices can be computed by multiplying elements of the first row of the first matrix with the first column of the second matrix then, add...
6 min read
It is very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
6 min read
Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs. To learn the pattern program, we must have...
15 min read
Java Convert float to String We can convert float to String in java using String.valueOf() and Float.toString() methods. Scenario It is generally used if we have to display float value in textfield because everything is displayed as a string in form. 1) String.valueOf() The String.valueOf() is an overloaded method. It can...
1 min read
In this program, we will create a circular linked list and sort the list in ascending order. In this example, we maintain two nodes: current which will point to head and index which will point to node to current. The first loop, keep track...
3 min read
In this program, we will create a doubly linked list and sort nodes of the list in ascending order. Original List: Sorted List: To accomplish this, we maintain two pointers: current and index. Initially, current point to head node and index will point to node to current....
7 min read
Java Convert Octal to Decimal We can convert octal to decimal in java using Integer.parseInt() method or custom logic. Java Octal to Decimal conversion: Integer.parseInt() The Integer.parseInt() method converts a string to an int with the given radix. If you pass 8 as a radix, it converts an octal...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India