Didart

Refactor Sum Of Odd Numbers

Sep 16th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class RefactorSumOfOddNumbers {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int number = Integer.parseInt(scanner.nextLine());
  8.  
  9.         int sum = 0;
  10.        for (int i = 0; i < number; i++) {
  11.              System.out.println(2 * i + 1);
  12.              sum += 2 * i;
  13.              sum++;
  14.          }
  15.         System.out.printf("Sum: %d%n", sum);
  16.     }
  17. }
  18.  
Advertisement
Add Comment
Please, Sign In to add comment