Advertisement
veronikaaa86

09. Sum of Odd Numbers

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