Click here to Skip to main content
16,023,124 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Write a program that keeps asking the user to enter the numbers between 0 and 999. Use a sentinel of your choice to stop taking numbers. What you need to do after getting all the numbers is to identify and display the most frequent digit that was entered. For example, if the user entered 3, 17, 81, 24, 354, 49, and 60, then the most frequent digit is 4.
Posted
Updated 27-Mar-10 14:58pm
v2

there is a very simple hack that yields the results almost instantly, you don't even need to convert the user's input to numbers, all it takes is to concatenate all inputs in one huge string; then count the number of zeroes, ones, twos, etc. which can be done by checking string length change caused by replacing a digit by an empty string. The problem can be solved in 10 lines of code!

:)
 
Share this answer
 
If you are sure you don't want to use arrays then you can try something like simply placing the numbers together in a string. You can then iterate through each character in the string and keep track of the count through a loop.

psudo code:
C#
private string allData;
public void GetData(string theNumber)
{
    allData += theNumber.ToString;
}
//after all the data is entered:
public string MostCounted()
{
    string currChar;
    int currCount;
    string highestCountedChar;
    int highestCharCount;
    for (int i = 0; i <= allData.Length - 1; i++) {
        currChar = allData.Substring(i, 1);
        for (int i = 0; i <= allData.Length - 1; i++) {
            if (currChar == allData.Substring(i, 1)) {
                currCount += 1;
            }
        }
        if (currCount > highestCharCount) {
            highestCountedChar = currChar;
            highestCharCount = currCount;
        }
        currCount = 0;
    }
    return highestCountedChar;
}
 
Share this answer
 
v2

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