You need to find which digit occurs most number of times across the four given input numbers.
input1, input2, input3 and input4 are the four given input numbers.
The program is expected to find and print the most frequent digit.
Example1 –
If input1=123, input2=234, input3=345, input4=673
We see that across these four numbers,
1, 5, 6 and 7 occur once,
2 and 4 occur twice, and
3 occurs four times.
Therefore, 3 is the most frequent digit and so the program must print 3
NOTE: If more than a digit occurs the same number of most times, then the highest of those digits should be the result. Below example illustrates this.
Example2 –
If input1=123, input2=456, input3=345, input4=5043
We see that
0, 1, 2 and 6 occur once, and
3, 4 and 5 occur thrice.
As there are three digits (3, 4 and 5) that occur most number of times, the result will be the highest (max) digit out of these three. Hence, the result should be 5
#include <iostream>
int main()
{
const int quanity = 4;
int numbers[quanity];
int digits[10] = {0};
int result;
int modify_number;
for (int i = 0; i < quanity; i++) {
std::cout << "Enter " << i + 1 << "-th element: " ;
std::cin >>numbers[i];
}
for (int i = 0; i < quanity; i++){
digits[numbers[i] % 10]= digits[numbers[i] % 10] +1;
modify_number = numbers[i];
while (modify_number > 9) {
modify_number = modify_number / 10;
digits[modify_number % 10] = digits[modify_number % 10] + 1;
}
}
result = 0;
for (int i = 1; i < 10; i++) {
if (digits[i] >= result)
result = i;
}
std::cout << "The most common digit in numbers:" << result;
return 0;
}
Comments
Leave a comment