Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

When c is the largest the program does not run

0 votes
asked Mar 1, 2019 by Blue Bird
import java.util.Scanner;
public class CompareThree2{

public static void main(String args[]){
int a,b,c;
Scanner sobjr=new Scanner(System.in);
System.out.println("enter three diffrent no.");
a=sobjr.nextInt();
b=sobjr.nextInt();
c=sobjr.nextInt();
if(a>b){
if(a>c)
{if(b>c)
System.out.println(a+" is largest and " +c+ " is the smallest number");
else
System.out.println(a+" is largest and " +b+ " is the smallest number");
}
}
else if(b>a)
{
if(b>c)
{if(a>c)
System.out.println(b+" is largest and " +c+ " is the smallest number");
else
System.out.println(b+" is largest and " +a+ " is the smallest number");
}
}
else if(c>a)
{
if(c>b)
{if(b>a)
System.out.println(c+" is largest and " +a+ " is the smallest number");
else
System.out.println(c+" is largest and " +b+ " is the smallest number");
}
}
else
System.out.println("numbers are not distinct");
}
}

1 Answer

0 votes
answered Mar 2, 2019 by Utkarsh Mishra
the condition for running inner-block of "if(c>a)" will never run until both "a" and "b" are equal and also "c>a". Since for different input value of "a" and "b" any one of the first two if blocks will run based upon the condition. Hence third "if" block would never run.Try implementing the following program where you check both of the conditions in a single if-block.

import java.util.Scanner;
public class Main
{

  public static void main (String args[])
  {
    int a, b, c;
    Scanner sobjr = new Scanner (System.in);
      System.out.println ("enter three diffrent no.");
      a = sobjr.nextInt ();
      b = sobjr.nextInt ();
      c = sobjr.nextInt ();
    if (a > b && a > c)
      {
            if (b > c)
              System.out.println (a + " is largest and " + c +
                      " is the smallest number");
            else
              System.out.println (a + " is largest and " + b +
                      " is the smallest number");
      }
    else if (b > a && b>c)
      {

            if (a > c)
              System.out.println (b + " is largest and " + c +
                      " is the smallest number");
            else
              System.out.println (b + " is largest and " + a +
                      " is the smallest number");
      }
    else if (c > a && c>b)
      {
            if (b > a)
              System.out.println (c + " is largest and " + a +
                      " is the smallest number");
            else
              System.out.println (c + " is largest and " + b +
                      " is the smallest number");
      }
    else
      System.out.println ("numbers are not distinct");
  }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...