In: Computer Science
9) Create a java programming where you will enter two numbers
and create only one method, which will return or display
addition, substraction, multiplication, division, average and check
which number is greater than the other.
Everything in one method and call it in main method.
Above program is to do calculations in one function and call it in the main method
Firstly , in main method, 2 integers are entered in console
and a function calculation is called with two arguments
As the function is called, control is moved inside the function
Inside the function, addition ,subtraction, multiplication , division, average manipulations are performed and printed in the console.
After that, number is compared which one is greater using If-else blocks and printed in the console.
_________________________
Below is the code in JAVA:
import java.util.*;
public class Main
{
static void calculation(int num1,int num2)
{
int sum,sub,mul,div,avg;
sum=num1+num2;
sub=num1-num2;
mul=num1*num2;
div=num1/num2;
avg=(num1+num2)/2;
System.out.println("Sum of 2 numbers: "+sum);
System.out.println("Subtraction of 2 numbers: "+sub);
System.out.println("Multiplication of 2 numbers: "+mul);
System.out.println("Division of 2 numbers: "+div);
System.out.println("Average of 2 numbers: "+avg);
if(num1>num2){
System.out.println(num1+" is greater");
}
else if(num1<num2){
System.out.println(num2+" is greater");
}
else{
System.out.println("Both numbers are equal");
}
}
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
System.out.println("Enter 2
numbers");
int num1,num2;
num1=scr.nextInt();
num2=scr.nextInt();
calculation(num1,num2);
}
}
Below is the code screenshot:
Below is the output screenshot: