This site works best with JavaScript enabled!

Example program (Java): Sum of all numbers from 1 to...

<< previous page


In this program, all the values are summed from 1 up to the entered value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;
import java.io.*;
class Testprogram {

public static void main(String [] args) {

	long eingabe1, summe;
	System.out.println("Please enter a number, to which all the values from 1 should be summed!");
	Scanner eingabe = new Scanner(System.in);
	eingabe1 = Integer.valueOf(eingabe.next());
	summe = (eingabe1*(eingabe1 + 1))/2;
	System.out.println ("The sum of all numbers from 1 to " + eingabe1 + " is: " + summe);

}
<< previous page
Share this page
Posted on 17.10.2011 | Last modified on 20.11.2016