Getting smallest n biggest num using while loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahmee
    New Member
    • Mar 2010
    • 39

    Getting smallest n biggest num using while loop

    this a program for getting smallest and biggest number in a integer list using a while loop.....one of guy ask it but i didnt find him on bytes....so if he find here is the code and also for other users


    Code:
    import javax.swing.*;
    class stringdemo
    { public static void main(String[] args) {  
    
    
    int num1=Integer.parseInt(JOptionPane.showInputDialog("enter num1"));
    int num2=Integer.parseInt(JOptionPane.showInputDialog("enter num2"));
    int num3=Integer.parseInt(JOptionPane.showInputDialog("enter num3"));
    int num4=Integer.parseInt(JOptionPane.showInputDialog("enter num4"));
    
    while(num1>0&&num2>0&&num3>0&&num4>0)
    {
    
    	
    if(num1>num4&&num1>num3&&num1>num2)
    {
    System.out.println("biggest num =" +num1);
    } else
    if(num2>num4&&num2>num3&&num2>num1)
    System.out.println("biggest num =" +num2);
    else
    if(num3>num4&&num3>num2&&num3>num1)
    System.out.println("biggest num =" +num3);	
    else
    if(num4>num3&&num4>num2&&num4>num1)
    System.out.println("biggest num =" +num4);
    						
    if(num1<num4&&num1<num3&&num1<num2)
    {
    System.out.println("smallest num =" +num1);
    } else
    if(num2<num4&&num2<num3&&num2<num1)
    System.out.println("smallest num =" +num2);
    else
    if(num3<num4&&num3<num2&&num3<num1)
    System.out.println("smallest num =" +num3);	
    else
    if(num4<num3&&num4<num2&&num4<num1)
    System.out.println("smallest num =" +num4);
    break;
    }}}
    Last edited by Atli; Sep 12 '10, 01:29 AM. Reason: Added [code] tags.
  • xploreraj
    New Member
    • Jan 2010
    • 49

    #2
    If users would enter numbers with their choice how many to enter, the how to modify the program? I mean the number of numbers to enter is in user's discretion.

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      ahmee I appreciate your interest in helping others. But we do not provide entire solution, please read our FAQ.

      Regards
      Dheeraj Joshi

      Comment

      • xploreraj
        New Member
        • Jan 2010
        • 49

        #4
        Thanks Dheeraj for giving time. But just tell me the concept, I am not interested in getting codes, as I can get them on Google, and neither I appreciate it. I just want to invoke my thought process and just the pre-requisite thing is some fuel from your side. Hope you understand.

        Regards

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Hi, ahmee :)

          Your insight and desire to help is greatly appreciated on this forum; however this post would be a lot better if you took the time to explain your code so that others can understand how to accomplish this.

          You cannot edit your post, but if you want to post a reply with an updated, more informative version of your original post I can edit your thread, remove all conversations regarding changes to it, and we can turn this into a good, helpful, article that we can add to the Java Insights forum.

          -Frinny

          Comment

          • ahmee
            New Member
            • Mar 2010
            • 39

            #6
            xploreraj this how you can enter number of your choice as much as you can and if you have any problems regarding program so can ask



            Code:
            import javax.swing.*;
            
                class BigSmal
            {
                public static void main(String[] args) {
            
                int[] a=new int [8];
                int[] b=new int [8];
                int j=0;
                String Output= " ";
            
                for(int i=0;i<=5;i++)
            {
                String enter=JOptionPane.showInputDialog("enter num");
                int y=Integer.parseInt(enter);
            
                String nter=JOptionPane.showInputDialog("enter num a");
                int x   =Integer.parseInt(nter);
            
                int c=Math.min(x,y); // method of class math to find min num
                int z=Math.max(x,y); // method of class math to find max num
                b[i]=z;   // array to store max numbers while the loop circles
                a[i]=c;   // array to store min numbers while the loop circles
            
                if(a[0]>=a[i])  
            {
                a[0]=a[i];
            }
            
                if(b[0]<=b[i])
            {
             	b[0]=b[i];
            }}
                Output+="Smallest number is = " +a[0]+"\n largest number is = " +b[0];
            
                JOptionPane.showMessageDialog(null,Output,"Note",JOptionPane.INFORMATION_MESSAGE);
            }}

            Comment

            • xploreraj
              New Member
              • Jan 2010
              • 49

              #7
              Hi ahmee, thanks a lot for the code.

              I am trying to understand and change the code, then I will post my doubts here.

              Regards,
              xploreraj :D

              Comment

              • ahmee
                New Member
                • Mar 2010
                • 39

                #8
                my pleasure.

                Comment

                Working...