Advertisement
Vladkoheca

SumOfEvenNums.java

Oct 25th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.48 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SumOfEvenNum {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         while (true) {
  8.             System.out.print("Enter a number: ");
  9.             int n = sc.nextInt();
  10.             int sum = 0;
  11.             for (int i = 2; i <= n; i += 2) {
  12.                 sum += i;
  13.             }
  14.             System.out.println("The sum of even numbers from 1 to " + n + " is: " + sum);
  15.         }
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement