Home > front end >  Given three distinct integers A, B and C, print the second largest number among them
Given three distinct integers A, B and C, print the second largest number among them

Time:02-05

when i am running this code i am getting correct answer but in codechef there showing wrong how??

here check question https://www.codechef.com/CCSTART2/SECLAR

public static void main (String[] args) throws java.lang.Exception
{
    // your code goes here
    Scanner in=new Scanner(System.in);
    int first=in.nextInt();
    int second=in.nextInt();
    int Third=in.nextInt();
    int temp=0;
    temp=Math.max(first,second);
    int tMax=Math.min(temp,Third);
    System.out.print(tMax);
    
}

CodePudding user response:

You need to find two things before you can find the middle value; you need the largest value and you need the smallest value. Currently, you don't find either. Consequently, you won't always find the correct middle value.

Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int max = Math.max(Math.max(a, b), c); // max of [a, b, c]
int min = Math.min(Math.min(a, b), c); // min of [a, b, c]
int middle = a   b   c - max - min; // sum everything, subtract the min and max
System.out.println(middle);

CodePudding user response:

this is one line solution for comparing 3 numbers using turnery oprator:

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner in=new Scanner(System.in);
        int first=in.nextInt();
        int second=in.nextInt();
        int Third=in.nextInt();
        int temp=0;
        temp=Math.max(first,second);
        int tMax=Math.min(temp,Third);
        System.out.println(tMax);
        
             
        
    }
}
  •  Tags:  
  • Related