Computer Science, asked by harshita44073, 1 year ago

write a program in java to accept two numbers and display their greatest

Answers

Answered by frederick
2

we can solve this program on 2 basis

1)on basis of mode of input

2)on basis of usage of function

1)ON BASIS OF MODE OF INPUT

a) by using Scanner class

import java.lang.Math;

class ex

{

void main()

{

int x,y,g;

System.out.println("Enter first number : ");

x=new Scanner(System.in).nextInt();

System.out.println("Enter second number : ");

y=new Scanner(System.in).nextInt();

g=Math.max(x,y);

System.out.println("Greatest  number : "+g);

}

}

b) by not using Scanner class

import java.lang.Math;

class ex

{

void main(int x , int y)

{

int g;

g=Math.max(x,y);

System.out.println("Greatest  number : "+g);

}

}

2) ON BASIS OF USAGE OF FUNCTION

a) using Math package

import java.lang.Math;

class ex

{

void main()

{

int x,y,g;

System.out.println("Enter first number : ");

x=new Scanner(System.in).nextInt();

System.out.println("Enter second number : ");

y=new Scanner(System.in).nextInt();

g=Math.max(x,y);

System.out.println("Greatest  number : "+g);

}

}

b) without using math package


class ex

{

void main()

{

int x,y,g;

System.out.println("Enter first number : ");

x=new Scanner(System.in).nextInt();

System.out.println("Enter second number : ");

y=new Scanner(System.in).nextInt();

if(x>y)

{

System.out.println("Greatest  number : "+x);

}

else

{

System.out.println("Greatest Number :"+y)

}

}

}

hope it helps

plz mark as brainliest

plz follow me


frederick: plz mark as brainliest
Answered by AskewTronics
0

Below are the python program and the output for the greatest number from two number:

Output :

If the user input as 3 and 4, then the output will be 4.

If the user input as 1 and 2, then the output will be 2.

Explanation:

Number_1=int(input("Enter the first number: "))#Take the first number.

Number_2=int(input("Enter the Second number: "))#take the second number.

if(Number_1>Number_2):#Compare the number.

   print("The Greatest number is:",Number_1)#print the Greatest number.

else:

   print("The Greatest number is",Number_2)#it also prints the Greatest number.

Code Explanation :

  • The above code is in python language, which takes the two numbers as input after instructing the user for input.
  • Then the if and else is used to test the condition to compare the number and find the greater one.
  • Then the print statement will print the greater number.

Learn More :

  • Python : https://brainly.in/question/14689905
Similar questions