Write a java program to find the sum of all even numbers between 1 to n - using while loop
import java.util.Scanner;
class App {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Ente n: ");
int n = keyBoard.nextInt();
int i=1;
int sum=0;
while(i<=n){
if(i%2==0){
sum+=i;
}
i++;
}
System.out.println("Sum: "+sum);
keyBoard.close();
}
}
Comments