Advertisement
CoineTre

JF-Lab13. Refactor Sum of Odd Numbers

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