Advertisement
CoineTre

JF-Lab 09. Sum of Odd Numbers

Jan 14th, 2021
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SumOfOddNumbers {
  4.     public static void main(String[] args) {
  5.         int input = new Scanner(System.in).nextInt();
  6.         int number = 0;
  7.         int counter =0;
  8.         int sum = 0;
  9.         while (counter < input){
  10.             number++;
  11.             if (number %2 ==1) {
  12.                 sum += number;
  13.                 System.out.println(number);
  14.                 counter++;
  15.             }
  16.         }
  17.         System.out.println("Sum: " + sum);
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement