Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a function that takes a number and returns the largest digit value in the 
number. For example, given 34567 the function returns 7, and given 28634 it 
returns 8.


What I have tried:

I can only do it by using "max"
Posted
Updated 22-Nov-20 6:13am

Start by writing a function that extracts each digit from the number.
When you have that working, loop through each digit comparing them:
1) Set a "max value so far" variable to a "not the biggest value" (integer minimum value for example, of the first digit minus one).
2) Loop through all digits
2.1) Compare them against the "max value so far".
2.1.1) If the digit is bigger, set "max value so far" to the digit.
2.2) Move on to next digit.
3) After the loop, the largest digit is "max value so far".

Try it: it's not complicated!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900