Write a program to find the greatest among 2 numbers using conditional operators

Share Your Love

Hello guys, welcome to Lingarajtechhub. Today we are going to discuss finding the greatest among 2 numbers using conditional operators.

Code:

package com.conditional_Operator;
import java.util.Scanner;
public class Greatest_Num_User_Inp
 {
public static void main(String[] args)
       {
             int s,p ,max;
             Scanner Sc = new Scanner(System.in);
             System.out.println("enter the no");
             s=Sc.nextInt();
             System.out.println("Enter the 2nd no");
             p=Sc.nextInt();
             max=(s>p)?s:p;
             System.out.println("The greatest among two no is " + max);
	}

}

Line-1:

package com.conditional_Operator;

→ it is a java package( com.conditional_Operator) which is a collection of classes and this package is created in a java project(Homework_operator)

Line-2:

import java.util.Scanner;

→Scanner is a class in java. util package used for obtaining the input of the      primitive types like int, double, etc. and strings

Line-3:

public class Greatest_Num_User_Inp

→Here Greatest_Num_User_Inp  is defined as a class which is a template used to create objects and to define object data types and methods.

→and it’s defined as public because it can be accessed anywhere.

Line-5:

public static void main(String[] args)

  • it is the main method in which we can execute a program
  • public: it is an access modifier of the main function which must be public so that the Jvm which is (Java Virtual Machine) can access and execute this method. 
  • If a method is not  public, then access is restricted and its showing an error
  • static is a keyword in which the main method must be static in which JVM can load the class into memory and call the main method without creating an instance 
  • void  it means no return type
  • main() is function
  • string[]args: string means a collection of characters,String[] args represents a collection of Strings ,that are separated by a space and can be typed into the program on the terminal directly

Line-7:

int s,p,max;

Here we Declare 3 variable s,p,max which is integer type

Line-8:

Scanner Sc = new Scanner(System.in);

Here scanner is a class and Sc is the instance of the object which is  new Scanner.System.in is an argument

Line-9:

System.out.println(“enter the no”);

It means System is a class name and out is an instance of the System println is print a new line

Line-10:

s=Sc.nextInt();

Here s is a variable that we take and Sc is our instance name which is create for  a object and nextInt() is a method method of a Scanner object that reads in a string of digits or char  and converts them into an int type.

Line-13:

max=(s>p)?s:p;

  • Here we apply the logic where (s>p)?s:p; is assign to max variable
  • (s>p)? s:p;  it means if the value of s>p ,if it’s true  then it shows ?s(Expression 1) if s>p is false then its jump to  expression no 2 (p)

Line-14:

System.out.println(“The greatest among two no is ” + max);

  • it means System is a class name and out is an instance of the System println is print a new line
  • +max ,+ means cocardinate or comma operator,and here max means (s>p)? s:p this expression result is assign to max variable then result is shown

Conclusion:

So in this article, you learn about writing a program to find the greatest among 2 numbers using conditional operators.

Share Your Love
Avatar photo
SONALIN SAHOO

Myself Sonalin Shaoo. I am quite happy to contribute here some java programming content and my friends Lipsa Mohanty ,Sushil Puhan, Aswin Kumar Nayak also help me to contribute their Opinion on this .

Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter