1. Member Join Date
Jan 2009
Posts
90
Rep Power
0

## any suggestions?

how do i get this program's output to display both largerst and the smallest digit.

i alwasys get either one of them.

Java Code:
```import java.util.Scanner;

public class hw1 {

public static void main (String[]args){

Scanner input =  new Scanner(System.in);

System.out.println("Enter 4-digit of integer: ");

int a = input.nextInt();

int b = a % 10;

int x = a/10;

int c = x % 10;

int y = x / 10;

int d = y % 10;

int z = y / 10;

int e = z % 10;

if (a>0){

System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}

if ((b>= c)&& ( c >= d)&&(d >= e)){
System.out.println("Largest digit value is "+ b);
}
else if ((c>= d)&&(d >= e)&&(e>= b)){
System.out.println("Largest digit value is "+ c);
}
else if ((d>= e)&& ( e>= b)&&(b >= c)){
System.out.println("Largest digit value is "+ d);
}
else if ((e>= b)&&(b >= c)&&(c >= d)){
System.out.println("Largest digit value is "+ e);
}

if ((b <= c)&&(c <= d)&&(d <= e)){
System.out.println("Smallest digit value is "+ b);
}
else if ((c <= d)&&(d <= e)&&(e <= b)){
System.out.println("Smallest digit value is "+ c);
}
else if ((d <=e )&&(e <= b)&&( b<= c)){
System.out.println("Smallest digit value is "+ d);
}
else if ((e <= b)&&(b <= c)&&(c <= d)){
System.out.println("Smallest digit value is "+ e);
}

}

}```  Reply With Quote

2. ## Observations and suggestions

Comment:
• The comparation of a>0 should be done at the very begining of the program (after the value of "a" is obtained).

Suggestions:
Your comparation for the largest/smallest is not correct.
• If I input 1234, it will only display the largest number
• If I input 4321 it will only display the lowest number and ...
• If I input 1539, neither is displayed
Try a different approach:
Compare the first digit against the rest of the digits. For example:
a=1537
• if b>c, then largest = b
• if not, then c is larger than b therefore largest = c
• then comparare "largest" against the next digit. Once you find a larger number, make it "largest" value
• Do the same for the "smallest" value

Luck,
CJSL  Reply With Quote

3. Member Join Date
Jan 2009
Posts
90
Rep Power
0

## what you are suggesting a little confusing to me.
if i put if(a>0) at the very beginning, it wouldnt work for the rest of the calculations. i thought you can't put e.g ( int b = a % 10; ) inside the if loop.

Java Code:
```		if (b> c){
if (b>d)
if (b>e)
System.out.println("Largest digit value is "+ b);
}```
Last edited by PureAwesomeness; 01-19-2009 at 12:47 AM.  Reply With Quote

4. ## samples...

What is the purpose of checking if a>0? To make sure that the user has entered a valid digit--- right? i was thinking of something on the terms of:
Java Code:
```int a = input.nextInt();
if (a>0)
{
int b = a % 10;
int x = a/10;
int c = x % 10;
int y = x / 10;
int d = y % 10;
int z = y / 10;
int e = z % 10;
.
.
.
}
else
{
System.out.println("Enter a valid 4-digit of integer (ex: 1234)");
return;
}```
Anyway... for the comparation, you have to use another variable, which I will call "largest" (some pseudo code):
Java Code:
```if b> c then
largest = b;
else
largest = c;
if largest < d then
largest = d;
else
if largest < e
largest = e;```
In the end the largest value is in the "largest" variable.
Just do the same for the smallest value.

Luck,
CJSL  Reply With Quote

5. Senior Member Join Date
Dec 2008
Posts
526
Rep Power
0

## Why don't you just use Math.min() or Math.max() to get the effect you want? I think it is more comfortable :)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•