Answer to Question #329831 in Java | JSP | JSF for Kei

Question #329831

Write a java program to find the sum of all even numbers between 1 to n - using while loop

1
Expert's answer
2022-04-17T13:53:48-0400
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();




	}




}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS