Program to find out middle number among three variables using only if else in java .
Interview Answers
Anonymous
24 Jun 2014
I done that program correct but HR said it is incorrect.
Anonymous
1 Jul 2014
share your answer too.
Anonymous
18 Jul 2018
there is the simple way to answer tjis.. we can just check the alsphabed in ascii code. and then compare.
Anonymous
28 Nov 2018
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in= new Scanner(System.in);
int [] arr=new int[3];
for (int i=0;i<3;i++)
{
arr[i]=in.nextInt();
}
int mid=MidCalc(arr);
System.out.println(mid);
}
public static int MidCalc(int [] arr)
{
if (arr[0]
Anonymous
8 Jan 2015
var A =10, B=5, C=15, mid=0, flag=0;
if(A>B && B>C)
mid = B;
else if ( A>C && C>B)
mid = C;
else if ( B>A && A>C)
mid = A;
else if ( B>C && C>A)
mid = C;
else if ( C>A && A>B)
mid = A;
else if ( C>B && B>AB)
mid = B;
else
print("two or more numbers have same value");
flag=1;
if(flag=0)
{
print("Middle number is "+mid);
}
please ignore any syntax discrepancy. Kindly see logic only.
1
Anonymous
14 Apr 2015
var A =10, B=5, C=15, mid=0, flag=0;
if(A>B && B>C || C>B && B>AB)
mid = B;
else if ( A>C && C>B || B>C && C>A)
mid = C;
else if ( B>A && A>C || C>A && A>B)
mid = A;
else
print("two or more numbers have same value");
flag=1;
if(flag=0)
{
print("Middle number is "+mid);
}
There is single problem that you increase the execution time with using unneccessary else if while you can use OR operator also.Your logic is right but the execution of its wrong.