Silviyaa

13. Refactor Sum of Odd Numbers

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