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!
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number: ";
cin>>n;
if((n%10)>(n%1000) && (n%10)>(n%1000)){
cout<<"leftmost";
}
else if((n%100)>(n%10) && (n%100)>(n%1000)){
cout<<"Middle";
}
else if((n%1000)>(n%10) && (n%1000)>(n%100)){
cout<<"Rightmost";
}
else{
cout<<"unknown";
}
}
Comments
Leave a comment