hi guys.
can u please help me in my project?
code for a java program that accepts input number and displays the largest among the individual digits of that input.
The sample output is here.

Input Number: 123456789
Largest Digit: 9

We cant still use arrays guys because we didnt discuss it yet.
Hope u can help me with this.
any loops can do.
thanks.

i tried nothing.
i really dont know.
hope u can help me with this.

Match the variable of each array input by a ">" sign & load them in another variable which is greater & after completion output them on the screen...

import java.util.Scanner;
class group{
public static void main(String arng[]){
int value[]= new int[5];
int temp,i;
Scanner data = new Scanner(System.in);
System.out.println("Enter 5 element of array" );
// Enhanced for loop
for(i=0; i < 5; i++ ) value[i] = data.nextInt();
// finding Largest number
temp = value[0];
for(i=0; i < 5; i++ )
{
if(temp > value[i])
continue;
else
temp = value[i];
}
System.out.println("Largest number in array is "+temp);
}
}

Output:-

Enter 5 element of array
25
154
28522
218
2564
Largest number in array is 28522

i tried this but how can i do it without arrays?
and in a single 10 digits input how can i get the largest?
like 9899823211
how can i get 9?

import java.util.Scanner;
class group{
public static void main(String arng[]){
int value[]= new int[5];
int temp,i;
Scanner data = new Scanner(System.in);
System.out.println("Enter 5 element of array" );
// Enhanced for loop
for(i=0; i < 5; i++ ) value[i] = data.nextInt();
// finding Largest number
temp = value[0];
for(i=0; i < 5; i++ )
{
if(temp > value[i])
continue;
else
temp = value[i];
}
System.out.println("Largest number in array is "+temp);
}
}

Output:-

Enter 5 element of array
25
154
28522
218
2564
Largest number in array is 28522

i tried this but how can i do it without arrays?
and in a single 10 digits input how can i get the largest?
like 9899823211
how can i get 9?

Edited 4 Years Ago by peter_budo: Post formatting

i tried this but how can i do it without arrays?

This is a simple program. Without an array where would you store your input from the console? With the help of arrays you can achieve your goals 1 - to find the largest number in a list of numbers 2 - find the largest digit in a very large number. If you only want to find the largest digit in a number - you can achieve this without using arrays.

Rather than read the number fron the console as an int, you could read it as a String, then use the charAt method to access each char in turn in a simple loop. You can compare each char with the previous largest char to find the overall largest (that part is like lines 13-18 in your latest code).

Look back at your code, you could modify it to work with your new requirement. You already know how to use Scanner class. Instead of accepting integer using nextInt(), change it to nextLine() which will accept the whole line of user input as String.

When you deal with each digit, you may use char data type instead of int. Assuming that a user will always enter valid input format (all digit number and no other character in between), you could iterate through each character of a string using a for-loop and charAt(i).

String input = data.nextLine();  // accept a whole string line from a user
char ch;  // place holder for a character
char tmp='';  // place holder for the highest digit number
// iterate through each character in the string
// the index starts from 0 up to n-1 (where n is the length of the string)
for (int i=0; i<input.length(); i++) {
  ch = input.charAt(i);  // obtain each character in the string by its index
  // the char data type can be compare using >, <, ==
  // so you simply compare to find the maximum, the way you do with integer
}
System.out.println(tmp);
This article has been dead for over six months. Start a new discussion instead.