write a complete Java program that defines a main method and another method call
ID: 3679520 • Letter: W
Question
write a complete Java program that defines a main method and another method called sum. method sum takes 2 integer values, say a and b, and returns the sum of all values between a and b. a must be less than b for the method to return the sum write a complete Java program that defines a main method and another method called sum. method sum takes 2 integer values, say a and b, and returns the sum of all values between a and b. a must be less than b for the method to return the sum write a complete Java program that defines a main method and another method called sum. method sum takes 2 integer values, say a and b, and returns the sum of all values between a and b. a must be less than b for the method to return the sumExplanation / Answer
import java.util.Scanner;
class HelloWorld{
public static void main(String args[]){
//Scanner object for capturing the user input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first number:");
//Stored the entered value in variable
int num1 = scanner.nextInt();
System.out.println("Enter the second number:");
int num2 = scanner.nextInt();
//Called the user defined function add
int sum = add(num1,num2);
System.out.println("Factorial of entered number is: "+sum);
}
static int add(int n,int m)
{
int result = 0;
while (n <=m)
{
result+=n;
n++;
}
return n;
}
}
in the above cod eto read the elements you can use
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println(“Enter the first number:”);
num1=Integer.parseInt(br.readLine());
System.out.println(“Enter the first number:”);
num2=Integer.parseInt(br.readLine());
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.