Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class var_27_task_2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- boolean isIncorrect;
- int n = 0;
- do {
- System.out.print("Enter the value of n : ");
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException exception) {
- isIncorrect = true;
- System.err.println("Input Error. Check if the data is correct.");
- }
- if (!isIncorrect && (n <= 0)) {
- isIncorrect = true;
- System.err.println("Input Error. Check if the data is correct.");
- }
- } while (isIncorrect);
- scanner.close();
- int SumOfNumbers = 0;
- int i;
- for (i = 1; i<=n; i++){
- SumOfNumbers += ((i-1)+i);
- }
- System.out.println("Sum of all numbers = " + SumOfNumbers);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement