Advertisement
Sauka1337

Untitled

Sep 25th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class var_27_task_2 {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         boolean isIncorrect;
  10.         int n = 0;
  11.        
  12.         do {
  13.             System.out.print("Enter the value of n : ");
  14.             isIncorrect = false;
  15.             try {
  16.                 n = Integer.parseInt(scanner.nextLine());
  17.             } catch (NumberFormatException exception) {
  18.                 isIncorrect = true;
  19.                
  20.                 System.err.println("Input Error. Check if the data is correct.");
  21.             }
  22.            
  23.            
  24.             if (!isIncorrect && (n <= 0)) {
  25.                 isIncorrect = true;
  26.                
  27.                 System.err.println("Input Error. Check if the data is correct.");
  28.             }
  29.         } while (isIncorrect);
  30.  
  31.         scanner.close();
  32.  
  33.         int SumOfNumbers = 0;
  34.         int i;
  35.  
  36.         for (i = 1; i<=n; i++){
  37.             SumOfNumbers += ((i-1)+i);
  38.         }
  39.        
  40.         System.out.println("Sum of all numbers = " + SumOfNumbers);
  41.  
  42.     }
  43.    
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement