Advertisement
Spocoman

12. Refactor Sum of Odd Numbers

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