sum of all integer numbers
时间限制:1000 ms | 内存限制:65535 KB
难度:0
-
描述
- Your task is to find the sum of all integer numbers lying between 1 and N inclusive.
-
输入
- There are multiple test cases.
The input consists of a single integer N that is not greater than 10000 by it's absolute value.
输出 - Write a single integer number that is the sum of all integer numbers lying between 1 and N inclusive. 样例输入
-
3
样例输出 -
6
- There are multiple test cases.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int start = scanner.nextInt();
int end = 1;
int count = 0;
// start有可能是比1小的数
if (start > end) {
int temp = start;
start = end;
end = temp;
}
for (int i = start; i <= end; i++) {
count += i;
}
System.out.println(count);
}
}
}