Java program to print sum of n natural numbers

Views: 160   

Following program shows you how to print sum of n natural numbers.
In this program we get number from user and shows sum of n natural numbers using following formula
Sum of n natural numbers = input X (input + 1)/2

import java.util.Scanner;

public class BasicMath17 {

    public static void main(String[] args) {

        int input;
        System.out.println("Enter a number:");

        Scanner in = new Scanner(;
        input = in.nextInt();

        int result = input * (input + 1) / 2;




Enter a number:
On By