Advertisement
veronikaaa86

09. Sum of Odd Numbers

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