Didart

Sum Of Odd Numbers

Sep 16th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SumOfOddNumbers {
  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.         int counter = 0;
  11.         for (int i = 1; i <= 100; i += 2) {
  12.             System.out.println(i);
  13.             counter++;
  14.             sum += i;
  15.             if (counter == n) {
  16.  
  17.                 System.out.printf("Sum: %d", sum);
  18.                 break;
  19.             }
  20.         }
  21.     }
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment