I know.. i am dumb, the simple logic is difficult for me..

get 5 numbers from user, and compare, which is largest and smallest, no array should be used.

please help?

step 1: read in the first number
step 2: set the biggest and smallest number equal to the first number

step 3: repeat next steps{
A: read in numberN
if numberN is bigger than biggest number, overwrite the value current in biggest number
if numberN is smaller than smallest number, overwrite the value current in smallest number
}

Are you restricted only not to use array or you are also restricted to use any Collections (ArrayList, List, Vector, Map)?
If you are allowed to use other Collections then no problem there I guess.

In any other even I would go with 3 variables, (min, max, current) and with for loop would do reading of entries from user, validating if entry is a number and then comparing to min and max from current.

Is it clear?

i cant use for, while any loop.. could u help with code?

I gave you everything you need to code it. whether you repeat the steps by using a loop or by programming the code (or calling a method) several times, does not change anything in the logic.

this is very basic stuff. have you tried writing any code for this yet?

i was stupid... cant understand just a small logic.. thanx any way.

i got it already ...

check it .. out by the way.. any other way please suggest...

//finding largest and smallest of five nos entered by user.

import java.util.Scanner;
public class problem224
{
	public static void main( String args[])
	{
		int n1,n2,n3,n4,n5,l,s;
		Scanner input = new Scanner(System.in);
		
		
	        System.out.println("Enter 5 nos to calculate largest and smallest:");
	        n1 = input.nextInt();
	        l=n1;
	        s=n1;
	        
	        n2 = input.nextInt();
	        if(l>n2){ 
	        s=n2;
	        }
	        else
	        l=n2;
	
	        n3 = input.nextInt();
	        if(l>n3) {
	        	s=n3;
	        } 
	        else
	        	l=n3;
	        
	        n4 = input.nextInt();
	        if(l>n4){
	        	s=n4;
	        }
	        else
	        	l=n4;
	        
	        n5 = input.nextInt();
	        if(l>n5)
	        {
	        	s=n5;
	        }
	        else
	        	l=n5;
	            
	        
	        System.out.println("largest number is: " +l+" and smallest number is: " +s);
	        

		
	}//end main		
} // end of class

Edited 6 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

this will not give you what you want ...
what you have programmed
for instance

n2 = input.nextInt();
if(l>n2){
s=n2;
}
else
l=n2;

here you say that, whatever number n2 is, if it's smaller than the largest number, it's automatically the smallest number.

so, for instance:

your first number would be 3
this would then become both your biggest and smallest number
then, you enter six.
since it is bigger then 3
your biggest number would be 6, your smallest number would remain 3
so far, no problems.
but, now you enter 4
it's smaller then 4
the biggest number remains 6, check
but you overwrite your smallest number without checking whether 4 is smaller

so, while you should keep 3 as the smallest number, you now set the value for smallest number to 4

what you have to do, for each input after the first is, check whether or not the number is bigger than the value stored in biggest,
and whether or not the value stored in the smallest is smaller or not.

for the first two inputs (initial and the second one) it won't give you any problems, but starting from the third, it could (and propably will) give you sloppy results

import javax.swing.*;
 import java.util.*;
 class stringdemo
 { public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("enter num1\n");
int num1 = input.nextInt();

System.out.print("enter num2\n");
int num2 = input.nextInt();

System.out.print("enter num3\n");
int num3 = input.nextInt();

System.out.print("enter num4\n");
int num4 = input.nextInt();

System.out.print("enter num5\n");
int num5 = input.nextInt();

// you can also enter number thorugh JOPptionpane method.

/*
 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"));*/

 if(num1>0&&num2>0&&num3>0&&num4>0&&num5>0)
 {

///// for getting biggest number////

 if(num1>num5&&num1>num4&&num1>num3&&num1>num2)
 {
 System.out.println("biggest num =" +num1);
 } else
 if(num2>num5&&num2>num4&&num2>num3&&num2>num1)
 System.out.println("biggest num =" +num2);
 else
 if(num3>num5&&num3>num4&&num3>num2&&num3>num1)
 System.out.println("biggest num =" +num3);
 else
 if(num4>num5&&num4>num3&&num4>num2&&num4>num1)
 System.out.println("biggest num =" +num4);
 else
 if(num5>num4&&num5>num3&&num5>num2&&num4>num1)
 System.out.println("biggest num =" +num5);

///// for getting smallest number////

 if(num1<num5&&num1<num4&&num1<num3&&num1<num2)
 {
 System.out.println("smallest num =" +num1);
 } else
 if(num2<num5&&num2<num4&&num2<num3&&num2<num1)
 System.out.println("smallest num =" +num2);
 else
 if(num3<num5&&num3<num4&&num3<num2&&num3<num1)
 System.out.println("smallest num =" +num3);
 else
 if(num4<num5&&num4<num3&&num4<num2&&num4<num1)
 System.out.println("smallest num =" +num4);
  else
 if(num5<num4&&num5<num3&&num5<num2&&num5<num1)
 System.out.println("smallest num =" +num5);
}
}}

Edited 6 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

Comments
Do not hang out the hero with such a crappy code. And don't you think that after 66 posts it would be helpful to learn how to use code tags?
The stupid! It burns!
That is not a helpful nor decent comment.

extemer, with all due respect ...
firstly: don't just hand-out code, it goes against the rules of this forum
secondly: what the hell???
I can assure you, his code, although not getting the right result in all cases, was (when structure and such matter..) way better than yours.
why giving him a 'solution' if it's not a decent one?
not only won't he look (or learn, for that matter) any further, because he's got your working code, but, even worse, he might even think the approach you used is a good one.

Comments
True words
Damn! you got there before me! :)

import javax.swing.*;
import java.util.*;
class stringdemo
{ public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("enter num1\n");
int num1 = input.nextInt();

System.out.print("enter num2\n");
int num2 = input.nextInt();

System.out.print("enter num3\n");
int num3 = input.nextInt();

System.out.print("enter num4\n");
int num4 = input.nextInt();

System.out.print("enter num5\n");
int num5 = input.nextInt();

// you can also enter number thorugh JOPptionpane method.

/*
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"));*/

if(num1>0&&num2>0&&num3>0&&num4>0&&num5>0)
{

///// for getting biggest number////

if(num1>num5&&num1>num4&&num1>num3&&num1>num2)
{
System.out.println("biggest num =" +num1);
} else
if(num2>num5&&num2>num4&&num2>num3&&num2>num1)
System.out.println("biggest num =" +num2);
else
if(num3>num5&&num3>num4&&num3>num2&&num3>num1)
System.out.println("biggest num =" +num3);
else
if(num4>num5&&num4>num3&&num4>num2&&num4>num1)
System.out.println("biggest num =" +num4);
else
if(num5>num4&&num5>num3&&num5>num2&&num4>num1)
System.out.println("biggest num =" +num5);

///// for getting smallest number////

if(num1<num5&&num1<num4&&num1<num3&&num1<num2)
{
System.out.println("smallest num =" +num1);
} else
if(num2<num5&&num2<num4&&num2<num3&&num2<num1)
System.out.println("smallest num =" +num2);
else
if(num3<num5&&num3<num4&&num3<num2&&num3<num1)
System.out.println("smallest num =" +num3);
else
if(num4<num5&&num4<num3&&num4<num2&&num4<num1)
System.out.println("smallest num =" +num4);
else
if(num5<num4&&num5<num3&&num5<num2&&num5<num1)
System.out.println("smallest num =" +num5);
}
}}

Wow, such a time consuming and convoluted solution.

All he needs is to add an else-if after each if(instead of else which is not needed). Checking if the new number is smaller than the smallest, then bigger than the biggest, if either is true, then set the smallest/largest to equal that number.

Edited 6 Years Ago by Akill10: n/a

import javax.swing.*;
 import java.util.*;

Potential import conflicts, as discussed in another thread. Use explicit imports.

System.out.print("enter num1\n");

Terrible UI - don't give the users your variable names, give them something they can use!

int num1 = input.nextInt();

If you're using nextInt(), try{} it and catch the exception

System.out.print("enter num2\n");
int num2 = input.nextInt();
[...]

Put this in a loop, don't type it in over and over.

if(num1>0&&num2>0&&num3>0&&num4>0&&num5>0)

Why? Why no negative numbers? And why just dump the user at the command line without so much as a by-your-leave? Why not validate the input on the way in, and give the poor sucker a chance to follow your un-stated rules?

if(num1>num5&&num1>num4&&num1>num3&&num1>num2)
 {
 System.out.println("biggest num =" +num1);
[lather, rinse repeat]

Oh my dear sainted maiden Aunt Petunia. To laugh, or to cry?

Note to original poster: do not, under any circumstances, ever do this. Not even as a joke. A programmer in my office just walked by, saw this on my screen, and threw himself out the window in despair... fortunately, I'm on the ground floor, so it was only embarrassing for him, but still...

But if you're going to use an if chain for any purpose, get some elses in there, for the love of all that walks, creeps, crawls, or flies. Show some respect for time.

Once again, a fine lesson in how not to do this. Keep 'em coming...

Edited 6 Years Ago by jon.kiparsky: n/a

Can you solve using my code please?

i am trying to do that.

Have a look at Stultuske's response to your posting. He's got good advice.
Your code is not very far from right.

Try it with test cases. Here are some that you can use:

1,2,3,4,5
5,4,3,2,1
1,5,2,4,3
-5,-3,-1, 0, 1
1,1,1,1,5
5,5,5,5,1
45,-,12,-8,0

Try your code on these - does it get the right answers? If not, what do you think it's doing wrong?

firstly: don't just hand-out code, it goes against the rules of this forum
secondly: what the hell???

It is not against the rules it is one of request, an announcement, for our members not to just simply hand-out solution as the original poster (OP) is unlikely to learn from it. If OP after initial request to work on solution and only ask specific questions wouldn't start working on it and continue begging for solution or perhaps does not come back at all then you know what type of student it is...

@extemer you better start reading forum rules or the doors of the forum will close for you soon due to number of infractions. Secondly, you better think about your code as System.out.println is not solution for everything.

This article has been dead for over six months. Start a new discussion instead.