Java program to find the largest & smallest number in n numbers without using arrays

前端 未结 8 1075
天命终不由人
天命终不由人 2020-12-22 14:22

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

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


        
相关标签:
8条回答
  • 2020-12-22 14:58

    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;
       }
    
    0 讨论(0)
  • 2020-12-22 15:00
    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

    0 讨论(0)
提交回复
热议问题