write a java program to accept two numbers from user and display the greatest number and its sum ,and average
Answers
Answer:
Explanation:
import java.util.*;
class j4
{
public static void main(String args[])
{
int a,b,sum;
float avg;
Scanner buf=new Scanner(System.in);
System.out.print("Enter first number : ");
a=buf.nextInt();
System.out.print("Enter second number : ");
b=buf.nextInt();
/*Calculate sum and average*/
sum = a+b;
avg = (float)((a+b)/2);
System.out.print("Sum : " +sum +"\nAverage : " +avg);
}
}
Answer:
import java.util.Scanner;
public class ProgramToAccept3Numbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number : ");
int num1 = sc.nextInt();
System.out.println("Enter the second number : ");
int num2 = sc.nextInt();
int sum = num1+num2;
float average = sum/2;
System.out.println("The sum of "+num1+" and "+num2+" is "+sum
+"\nThe average of "+num1+" and "+num2+" is "+average);
if(num1>num2){
System.out.println(num1+" is greater than "+num2);
}else if(num1<num2){
System.out.println(num2+" is greater than "+num1);
}else{
System.out.println("Both numbers "+num1+" and "+num2+" are equal!");
}
}
}
Explanation: