grach

Sum of odd numbers - Fundamentals Syntax

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