In this program, you'll learn to calculate the sum of natural numbers using for loop and while loop in Java. Show To understand this example, you should have the knowledge of the following Java programming topics:
The positive numbers 1, 2, 3... are known as natural numbers and its sum is the result of all numbers starting from 1 to the given number. For n, the sum of natural numbers is: 1 + 2 + 3 + ... + n Example 1: Sum of Natural Numbers using for loop
Output Sum = 5050 The above program loops from 1 to the given num(100) and adds all numbers to the variable sum. You can solve this problem using a while loop as follows: Example 2: Sum of Natural Numbers using while loop
Output Sum = 1275 In the above program, unlike a for loop, we have to increment the value of i inside the body of the loop. Though both programs are technically correct, it is better to use for loop in this case. It's because the number of iteration (up to num) is known. Visit this page to learn how to find the sum of natural numbers using recursion. Improve Article Save Article Improve Article Save Article Print first N natural numbers using an iterative approach i.e. using for loop. For loop has three parameters initialization, testing condition, and increment/decrement. Input: N = 10 Output: First 10 Numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Sum of first 10 Natural Number = 55 Input: N = 5 Output: First 5 Numbers = 1, 2, 3, 4, 5 Sum of first 5 Natural Number = 15 Approach
Below is the implementation of above approach Java
Output First 10 Numbers = 1 2 3 4 5 6 7 8 9 10 Sum of first 10 Natural Number = 55 Time Complexity: O(n) Alternate Approach
Below is the implementation of the above approach Java
Output First 5 Numbers = 1 2 3 4 5 Sum of first 5 Natural Number = 15 Time Complexity: O(n) Auxiliary Space: O(1) as it is using constant space for variables How to print sum of even number in Java?int sum = 0; for (int i = 1; i <= 10; i++) { // Logic for sum of EVEN if (i % 2 == 0) { sum = sum + i; } } System. out. println("Sum of all even from 1 to 10 is: " + sum);
How to print n natural number in Java?Java Program to Print First N Natural Numbers using Recursion. import java.util.Scanner;. public class Natural.. public static void main(String[] args). int n;. Scanner s = new Scanner(System. in);. System. out. print("Enter any number:");. How to display sum in Java?Sum of Two Numbers Using Command Line Arguments in Java. public class SumOfNumbers4.. public static void main(String args[]). int x = Integer.parseInt(args[0]); //first arguments.. int y = Integer.parseInt(args[1]); //second arguments.. int sum = x + y;. System.out.println("The sum of x and y is: " +sum);. How to print sum of odd and even numbers in Java?Java Program to Find the Sum of Even and Odd Numbers. import java.util.Scanner;. public class Sum_Odd_Even.. public static void main(String[] args). int n, sumE = 0, sumO = 0;. Scanner s = new Scanner(System. in);. System. out. print("Enter the number of elements in array:");. |