Write a java program to accept two integers . Find and print the greatest from them otherwise print equal if both are same.
Answers
Answer:
import java.util.*;
class integer{
public static void main (String ar[]){
Scanner sc=new Scanner (System.in);
System.out.println("Enter 2 numbers");
int a=sc.nextInt();
int b=sc.nextInt();
if(a==b)
System.out.println("Equal");
else if (a>b)
System.out.println(a+" is greater");
else
System.out.println(b+" is greater");
}
}
Another Approach:-
import java.util.*;
class integer{
public static void main (String ar[]){
Scanner sc=new Scanner (System.in);
System.out.println("Enter 2 numbers");
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println((a>b)?a+" is greater":(b>a)?b+" is greater":" Equal");
}
}
Sample I/O:-
Enter 2 numbers
23
27
27 is greater
Enter 2 numbers
23
21
23 is greater
Enter 2 numbers
23
23
Equal
Hint:-
- Use if-else or ternary operator
Note:-
- 1st approach is done using if-else construct
- 2nd approach is done using ternary operator
Java Program::
import java.util.Scanner;
public class Brainly {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int n = sc.nextInt();
if (i > n) {
System.out.println(i + " is greater");
}
else if (i < n) {
System.out.println(n + " is greater");
}
else {
System.out.println("Both are equal");
}
}
}
Sample I/O::
==> 2
5
==> 5 is greater
==> 2
2
==> Both are equal