Write a Java Program to find the Sum Of Even Numbers from Given range
Basic Java programs. All programs done in very simple ways. It helps beginners to grow their programming skills and build a programming logic.
Write a Java Program to find the Sum Of Even Numbers from Given range
import java.util.Scanner; public class SumOfEvenNumbers { public static void main(String[] args) { Scanner scan = new java.util.Scanner(System.in); System.out.println("Enter the starting number: "); int n1=scan.nextInt(); System.out.println("Enter the end number: "); int n2=scan.nextInt(); int sum=0; for(int i=n1; i<=n2; i++) { if(i%2==0) { System.out.println(i); sum=sum+i; } } System.out.println("Sum of Even Numbers from given range: "); System.out.println("= "+sum); } }Output:
No comments:
Post a Comment
If you have any doubts, please let me know