write a Java program to design a method called: void sumeven(int a.int b)
to take two numbers from the main()method then it will display the sum of the even number as well as how many evens it found.
Answers
Answered by
4
The given problem is solved using language - Java.
import java.util.Scanner;
public class Brainly{
static void sumeven(int a,int b){
int s=0;
for(int i=a;i<=b;i++){
if(i%2==0)
s+=i;
}
System.out.println("Sum of even numbers in the given range is: "+s);
}
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
int a,b;
System.out.print("Enter starting range: ");
a=sc.nextInt();
System.out.print("Enter ending range: ");
b=sc.nextInt();
sc.close();
sumeven(a,b);
}
}
- A funtion sumeven() is created which takes the starting and ending limit from the user. Then a loop iterates which calculates the sum of the even numbers in the range and displays it.
- The function is then called inside main after taking input from the user.
See attachment for output.
Attachments:
anindyaadhikari13:
Thanks for the brainliest!
Similar questions
Social Sciences,
16 days ago
English,
9 months ago