Monday, 31 August 2020

Sum Of Even Numbers from Given range

Write a Java Program to find the Sum Of Even Numbers from Given range

Input: 10 to 20
output: 90

Code in Java:

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:
Enter the starting number: 
1
Enter the end number: 
10
2
4
6
8
10
Sum of Even Numbers from given range: 
= 30


Write a JAVA program to Find the Sum Of First N Natural Numbers

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32