Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SumOfEvenNum {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.print("Enter a number: ");
- int n = sc.nextInt();
- int sum = 0;
- for (int i = 2; i <= n; i += 2) {
- sum += i;
- }
- System.out.println("The sum of even numbers from 1 to " + n + " is: " + sum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement