# any suggestions?

• 01-18-2009, 09:42 PM
PureAwesomeness
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.

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); } } }```
• 01-18-2009, 10:07 PM
CJSLMAN
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
• 01-19-2009, 01:23 AM
PureAwesomeness
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.

Code:

```                if (b> c){                         if (b>d)                                 if (b>e)                 System.out.println("Largest digit value is "+ b); }```
• 01-19-2009, 03:55 AM
CJSLMAN
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:
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):
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
• 01-19-2009, 08:34 AM
Webuser
Why don't you just use Math.min() or Math.max() to get the effect you want? I think it is more comfortable :)