Answer to Question #318712 in C++ for John

Question #318712

Where's the Biggest One?

by CodeChum Admin

We've already tried comparing 3 numbers to see the largest among all, so let's try a more complicated mission where we locate where among the 5-digit integer is the largest one. There are only 4 possible cases for this mission:

  • if the largest digit is the first digit, print "Leftmost"
  • if the largest digit is the third digit, print "Middle"
  • if the largest digit is the last digit, print "Rightmost"
  • if none of the above is correct, print "Unknown"

Now, show me how far you've understood your lessons!

Input

A line containing a five-digit integer.

14632

Output

A line containing a string.

Middle
1
Expert's answer
2022-03-26T12:40:31-0400
#include <iostream>
using namespace std;


int main() {
    int n;
    int d, max_d, i, i_max;


    cin >> n;


    d = n % 10;
    n /= 10;
    i = 1;
    max_d = d;
    i_max = i;


    while (i < 5) {
        i++;
        d = n % 10;
        n /= 10;
        if (d > max_d) {
            max_d = d;
            i_max = i;
        }
    }


    if (i_max == 5) {
        cout << "Leftmost";
    }
    else if (i_max == 3) {
        cout << "Middle";
    }
    else if (i_max == 1) {
        cout << "Rightmost";
    }
    else {
        cout << "Unknown";
    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS