Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
enter 10 numbers separated by comma(,) in one text box and find greatest number among them using button. Please help me out guys. Thanks in advance.
Posted
Comments
akhil2 14-Apr-12 12:40pm    
thanks guys for solution. This is not my homework. My friend got this question in his interview. I wants to help him. I tried but i couldn't do.

No one will do your homework for you - however, here is a hint ->
Use the String.Split command to split the string (inside the textbox) on the comma.
The result will be an array of 10 numbers,

Simply sort the array using any sort method.
 
Share this answer
 
v2
Comments
VJ Reddy 14-Apr-12 12:29pm    
Good hint. 5!
Abhinav S 14-Apr-12 23:34pm    
Thank you.
Since this sounds like homework, I won't give you code!
It's not difficult though:
1) Put a TextBox and a Button on your form. Give them sensible names - this is worth getting into the habit of right from the beginning.
2) Double click the button to create the default event handler - the click event. This will take you to the code.
3) Read the data from the Textbox.Text property[^], and use the string.Split method[^] to separate them at the comma.
4) Loop though each string in the separated list, converting it to an integer with the int.Parse[^] or int.TryParse[^] method. Compare it with the current maximum value, if it is creater, then it becomes teh current maximum.
5) After the loop, the current maximum is the largest number.
 
Share this answer
 
Comments
VJ Reddy 14-Apr-12 12:29pm    
Nice guidance. 5!
Hi,
This is the code to do that:

C#
public int GetGreatest(string numbers)
{
   string[] arr = numbers.Split(',');
   Array.Sort(arr);
   return int.Parse(arr[arr.length-1]);
}


I hope it helps,
Cheers.
 
Share this answer
 
v2
I think one important point is to compare after converting the number in text format to number.
C#
string numbers = "22, 10, 1, 35, 20, 25, 85, 25, 100, 5";

var max1 = numbers.Split(',').Max (n => Int32.Parse(n) );
Console.WriteLine ("Max number compared as integer: " + max1);

var max2 = numbers.Split(',').Max (n=> n.Trim());
Console.WriteLine ("Max number compared as text: " + max2);

//Output
//Max number compared as integer: 100
//Max number compared as text: 85

this is because text will be compared from left to right, so 100 will come before 85.
 
Share this answer
 
v2
Comments
ProEnggSoft 17-Apr-12 22:54pm    
Good point. 5!
VJ Reddy 18-Apr-12 0:17am    
Thank you.

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