Advertisement
myrdok123

09. Sum of Odd Numbers

May 15th, 2024
783
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1. package ProgramingBasicsOverview;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P09_SumOfOddNumbers {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.        
  11.         int number = Integer.parseInt(scanner.nextLine());
  12.  
  13.         int currentNumber = 1;
  14.  
  15.         int sum = 0;
  16.  
  17.         for (int i = 1; i <= number ; i++) {
  18.  
  19.             System.out.println(currentNumber);
  20.             sum += currentNumber;
  21.             currentNumber += 2;
  22.  
  23.  
  24.         }
  25.  
  26.         System.out.println("Sum: " + sum);
  27.     }
  28. }
  29.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement