-10

I could get the largest without using arrays but, unable to get the smallest one.

    public static void main(String[] args)
    {
        int smallest=0;
        int large=0; 
        int num;

        System.out.println("enter the number");

        Scanner input=new Scanner(System.in);

        int n=input.nextInt();

        for(int i=0;i<n;i++)
        {
          num=input.nextInt();
          if(num>large)
          {
           large=num;
          }
          System.out.println("the largest is:"+large);
          //gives the largest number in n numbers

code for the smallest..

     if(i==0&&num>0)
         small=num;
     if(num<small)
        small=num;
     System.out.println(small);

      }
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
user3168844
  • 23
  • 2
  • 2
  • 6
  • Your code work for me after changing `small` to `smallest`. – Pshemo Feb 21 '14 at 05:24
  • 1
    You couldn't figure the condition for smallest number when you wrote that if block for largest number? – Abhishek Tyagi Aug 19 '16 at 13:50
  • Your logic for the smallest will fail if all the input numbers are greater than zero. Your logic for the greatest will fail if all the input numbers are less than zero. You can fix this by initializing `smallest` and `large` to be the first number of the input. – starball Oct 08 '22 at 02:17
  • 2
    I'm voting to close because of misleading title, problem partially caused by typo, and no examples of inputs and expected outputs (which could have also helped you discover your initialization/bounds mistake I mentioned above). – starball Oct 08 '22 at 03:37
  • I didn't realize this earlier, but the end goal of this question is almost the same as [this other one](https://stackoverflow.com/q/19671453/11107541) which was asked just 4 months before this one, where it wouldn't have taken much adjustment to learn from. I would have voted to close as a duplicate if I had caught that earlier. – starball Oct 10 '22 at 23:51

8 Answers8

0

Try this :

int smallest = Integer.MAX_VALUE;
for(int i=0;i<n;i++)
{
   num=input.nextInt();
   if(num>large)
   {
       large=num;
   }
   if(num<smallest){
       smallest=num;
   }
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47
-1
public static void main(String[] args) {
    int smallest = 0;
    int large = 0;
    int num;
    System.out.println("enter the number");//how many number you want to enter
    Scanner input = new Scanner(System.in);
    int n = input.nextInt();
    num = input.nextInt();
    smallest = num; //assume first entered number as small one
    // i starts from 2 because we already took one num value
    for (int i = 2; i < n; i++) {
        num = input.nextInt();
        //comparing each time entered number with large one
        if (num > large) {
            large = num;
        }
        //comparing each time entered number with smallest one
        if (num < smallest) {
            smallest = num;
        }
    }
    System.out.println("the largest is:" + large);
    System.out.println("Smallest no is : " + smallest);
}
Simon Schubert
  • 2,010
  • 19
  • 34
sivaramaraju
  • 352
  • 2
  • 8
-5

Try this...This simple

import java.util.Scanner;

class numbers
{
   public static void main(String args[])
   {
      int x, y, z;
      System.out.println("Enter three integers ");
      Scanner in = new Scanner(System.in);

      x = in.nextInt();
      y = in.nextInt();
      z = in.nextInt();

      if ( x > y && x > z )
         System.out.println("First number is largest.");
      else if ( y > x && y > z )
         System.out.println("Second number is largest.");
      else if ( z > x && z > y )
         System.out.println("Third number is largest.");
      else   
         System.out.println("Entered numbers are not distinct");
   }
}
-5
public class Main {
    public static void main(String[] args) {
        int i = 10;
        int j = 20;
        int k = 5;
        int x = (i > j && i > k) ? i : (j > k) ? j : k;
        int y = (i < j && i < k) ? i : (j < k) ? j : k;
        System.out.println("Largetst Number : "+x);
        System.out.println("Smallest Number : "+y);
    }
}

Output:
Largetst Number : 20
Smallest Number : 5

Ram Pukar
  • 1,583
  • 15
  • 17
-6

Try the code mentioned below

public static void main(String[] args) {
    int smallest=0; int large=0; int num;
    System.out.println("enter the number");
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    num=input.nextInt();
    smallest = num;
    for(int i=0;i<n-1;i++)
        {
            num=input.nextInt();
            if(num<smallest)
            {
                smallest=num;
            }
        }
        System.out.println("the smallest is:"+smallest);
}
Varun
  • 583
  • 5
  • 12
-6

@user3168844: try the below code:

import java.util.Scanner;

public class LargestSmallestNum {

    public void findLargestSmallestNo() {

        int smallest = Integer.MAX_VALUE;
        int large = 0;
        int num;

        System.out.println("enter the number");

        Scanner input = new Scanner(System.in);

        int n = input.nextInt();

        for (int i = 0; i < n; i++) {

            num = input.nextInt();

            if (num > large)
                large = num;

            if (num < smallest)
                smallest = num;

            System.out.println("the largest is:" + large);
            System.out.println("Smallest no is : "  + smallest);
        }
    }

    public static void main(String...strings){
        LargestSmallestNum largestSmallestNum = new LargestSmallestNum();
        largestSmallestNum.findLargestSmalestNo();
    }
}
Suneet Bansal
  • 2,664
  • 1
  • 14
  • 18
-6
import java.util.Scanner;

public class LargestSmallestNum {

    public void findLargestSmallestNo() {

        int smallest = Integer.MAX_VALUE;
        int large = 0;
        int num;

        System.out.println("enter the number");

        Scanner input = new Scanner(System.in);

        int n = input.nextInt();

        for (int i = 0; i < n; i++) {

            num = input.nextInt();

            if (num > large)
                large = num;

            if (num < smallest)
                smallest = num;

            System.out.println("the largest is:" + large);
            System.out.println("Smallest no is : "  + smallest);
        }
    }

    public static void main(String...strings){
        LargestSmallestNum largestSmallestNum = new LargestSmallestNum();
        largestSmallestNum.findLargestSmalestNo();
    }
}
ArnonZ
  • 3,822
  • 4
  • 32
  • 42
albert
  • 1
-6
import java.util.Scanner;

public class LargestSmallestNumbers {

    private static Scanner input;

    public static void main(String[] args) {
       int count,items;
       int newnum =0 ;
       int highest=0;
       int lowest =0;

       input = new Scanner(System.in);
       System.out.println("How many numbers you want to enter?");
       items = input.nextInt();

       System.out.println("Enter "+items+" numbers: ");


       for (count=0; count<items; count++){
           newnum = input.nextInt();               
           if (highest<newnum)
               highest=newnum;

           if (lowest==0)
               lowest=newnum;

           else if (newnum<=lowest)
               lowest=newnum;
           }

       System.out.println("The highest number is "+highest);
       System.out.println("The lowest number is "+lowest);
    }
}
Roru
  • 3
  • 3