Computer Science, asked by hashirmuhammad35, 1 month ago

wtite a program to enter two numbers ‘a’ and ‘b' find the greater number​

Answers

Answered by DüllStâr
82

Required program:

  • By using if else statements:

import java.util.*;

public class BigSmall

{

public static void main (Sting [] args)

{

Scanner sc = new Scanner (System.in);

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

int a = sc.nextInt();

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

int b = sc.nextInt();

if(a>b)

System. out.println ("Greater number is :"+a);

else

System. out.println ("Greater number is:"+b);

}

}

Input :

Enter first number:

6

Enter second number:

9

Output:

Enter first number:

6

Enter second number:

9

Greater number is: 9

Or:

  • By using Math.max()

import java.util.*;

public class MaxMin

{

public static void main (Sting [] args)

{

Scanner sc = new Scanner (System.in) ;

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

int a = sc.nextInt();

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

int b = sc.nextInt();

int max = Math.max(a,b);

System. out.println("Greater number is:"+max);

}

}

Input :

Enter first number:

6

Enter second number:

9

Output :

Enter first number:

6

Enter second number:

9

Great number is : 9

Note:-

  • What is max function?

This function is used to find greatest number between two numbers.

  • What is min function?

This function is used to find smallest number between two numbers.

  • What is if else statement?

if else statement is a conditional statement in which if the condition result comes true then block of statements will be executed under condition "if", otherwise else block statements will execute.

Similar questions