Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, November 15, 2017

3 Ways to Find Sum of First n numbers program in Java

In this post, You will learn today today how to find sum of first n numbers in java using while loop, for loop and mathematics formula(Optimized).

For this program, we have to input the natural number n value which you want to get the sum. So that Our program will get sum of first n numbers. This is basic program for freshers or who are studying engineering in their lab programs.



Example 1:
number = 10
sum = 55

Example 2:
number = 100
sum = 5050


Following are the example programs to find sum of first n or 10 or 100.

1. Using For Loop:

Finding n numbers sum using for loop.


package blog.java.w3schools.sum.numbers;
import java.util.Scanner;

public class SumNForLoop {

 public static void main(String[] args) {

  System.out.println("Enter a number : ");
  Scanner scanner = new Scanner(System.in);
  int number = scanner.nextInt();
  int sum = 0;
  for (int i = 0; i <= number; i++) {
   sum = sum + i;
  }

  System.out.println("Final sum of first " + number + " numbers using For loop is " + sum);
 }
}


Output:


Enter a number : 10
Final sum of first 10 numbers using For loop is 55

The above program takes input of n values and loop using for loop from 0 to n.

For loop examples programs.

2. Using While loop.


Finding first n numbers sum using while loop.


public class SumNWhileLoop {
 public static void main(String[] args) {

  System.out.println("Enter a number : ");
  Scanner scanner = new Scanner(System.in);
  int number = scanner.nextInt();
  int sum = 0;
  int i = 0;
  while (i <= number) {
   sum = sum + i;
   i++;
  }
  System.out.println("Final sum of first " + number + " numbers using While loop is " + sum);
 }
}


Output:

Enter a number : 10
Final sum of first 10 numbers using While loop is 55


While loop examples programs.

3. Using Arithmetic formula:


In high school, you may have learnt formula or theorem to get sum of first n numbers. Now we will write a optimized program using the formula.

Formula : n*(n+1)/2

Compared to first and second options, this is requires less to calculate the sum.
First two takes O(n) time complexity but using formula it is in O(1).


public class SumNFormula {
 public static void main(String[] args) {

  System.out.println("Enter a number : ");
  Scanner scanner = new Scanner(System.in);
  int number = scanner.nextInt();
  int sum = 0;
  sum = number * (number + 1) / 2;
  System.out.println("Final sum of first " + number + " numbers using formula is " + sum);
 }
}


Output:


Enter a number : 100
Final sum of first 100 numbers using formula is 5050


Summary:

By using these three ways or methods, we can write a program to find the sum and will give the same result but the better way to find the sum is using the arithmetic formula. This question is frequently asked for experienced programmers to check optimization process.


No comments:

Post a Comment

Please do not add any spam links in the comments section.