write a program in java what will show the greater number among three numbers use method
Answers
Answered by
1
Take two integer variables, say A, B& C.
Assign values to variables.
If A is greater than B & C, Display A is the largest
value.
If B is greater than A & C, Display B is the largest value.
If C is greater than A & B, Display A is the largest value.
Otherwise, Display A, B & C are not unique values.
Answered by
1
I am answering it in case you have to input three numbers
import java.util.Scanner;
class greater
{
public static void main(String args[])
{
Scanner obj = new Scanner(System.in);
System.out.println(“ENTER THE THREE NUMBERS ”);
int a,b,c;
a = obj.nextInt();
b = obj.nextInt();
c = obj.nextInt();
System.out.println(“THis STATEMENT IS NOT FOR THE PROGRAM, I ADDED IT TO EXPLAIN YOU .... Now everything before this statement has been used to input the three numbers.....If you dont have to input it you can use 3 variables with 3 values....After this one there are two ways to determine the greater number....Either by using ternary operator of by using if else statement.....IN the program i have used TErnary operator but i will also explain how to do it with the help of if-else statement after this program as i dont want to interrupt in between the program ”);
int f = (a>b)? a : b;
int g = (f>c)? f : c;
System.out.println(“THE GREATEST NUMBER IS ” + g);
}
}
Here the program has ended
You could have used if-else like:
if (a>b){
f = a;
}
else if(b>a){
f = b;
};
if (f>c){
g = f;
}
else if (c>f){
g = c;
};
System.out.println(“THE GREATEST NUMBER IS” + g);
I have tried my best to explain the question to you, so please leave a thanks and if my answer helped you,please mark it as the brainliest.
Thank you and hope my answer helps you out
import java.util.Scanner;
class greater
{
public static void main(String args[])
{
Scanner obj = new Scanner(System.in);
System.out.println(“ENTER THE THREE NUMBERS ”);
int a,b,c;
a = obj.nextInt();
b = obj.nextInt();
c = obj.nextInt();
System.out.println(“THis STATEMENT IS NOT FOR THE PROGRAM, I ADDED IT TO EXPLAIN YOU .... Now everything before this statement has been used to input the three numbers.....If you dont have to input it you can use 3 variables with 3 values....After this one there are two ways to determine the greater number....Either by using ternary operator of by using if else statement.....IN the program i have used TErnary operator but i will also explain how to do it with the help of if-else statement after this program as i dont want to interrupt in between the program ”);
int f = (a>b)? a : b;
int g = (f>c)? f : c;
System.out.println(“THE GREATEST NUMBER IS ” + g);
}
}
Here the program has ended
You could have used if-else like:
if (a>b){
f = a;
}
else if(b>a){
f = b;
};
if (f>c){
g = f;
}
else if (c>f){
g = c;
};
System.out.println(“THE GREATEST NUMBER IS” + g);
I have tried my best to explain the question to you, so please leave a thanks and if my answer helped you,please mark it as the brainliest.
Thank you and hope my answer helps you out
Similar questions