Trying to solve basic programming problem (logic based).
finding the largest digit in four variables.
i have done the program w/ several inputs but when it comes to specifically
6 13 9 10 instead of 13 being the output. 10 becomes it output
#include <iostream>
usingnamespace std;
int max_of_four(int a, int b, int c, int d) {
int largest = a;
if (b > a)
{
largest = b;
}
if (c > b) {
largest = c;
}
if (d > c){
largest = d;
}
cout << largest;
}
int main() {
int num1,num2,num3,num4;
cin >> num1 >> num2 >> num3 >> num4;
max_of_four(num1,num2,num3,num4);
return 0;
}
#include <iostream>
usingnamespace std;
void max_of_four(int a, int b, int c, int d) {
int largest = a;
if (b > a)
{
largest = b;
}
if (c > largest) {
largest = c;
}
if (d > largest){
largest = d;
}
cout << largest;
}
int main() {
int num1,num2,num3,num4;
cin >> num1 >> num2 >> num3 >> num4;
max_of_four(num1,num2,num3,num4);
return 0;
}
the largest digit is 9 in your example.
what I suggest is to avoid all the comparisons etc ..
bool digits[10] = {false};
for all the numbers
get a digit
digits[numbers_digit] = true
then back iterate it
for(i = 9; i >=0; i--)
if digits[i] //this is your answer, stop loop and tell user all about it
can you do this? The least digit in a number is num%10. You can then remove that digit by num /= 10. Do this until num == 0.
fun stuff: you can stop looking if you ever find a 9 anywhere.