Finding the Largest Digit in C++

Viewed 1666
  1. Input a 3-digit integer.
  2. Print the largest digit in the integer (Tip: use % 10 to get the rightmost digit, and / 10 to remove the rightmost digit).
Input: 173
Expected Output: 7

We were given this activity 2 days old and still couldn't solve this mystery. Here's my own code which doesn't match the given expected output above:

#include<iostream>
using namespace std;

int main() {

   int num, result;

   cin >> num;
    if(num > 0) {
        result = num % 10;
        num / 10;
        cout << result;
    }

   return 0;
}
5 Answers

You separate only the last digit, but need to check all - just add a loop. Also num / 10 does nothing.

 maxdigit = 0;
 while (num > 0) {
      maxdigit = max(maxdigit, num % 10);
      num /= 10;
  }
 cout << maxdigit;

Different way to solve the problem. Take the input as a string. You can handle much larger numbers and the string is already decomposed into digits. You barely have to think. Just work through the string character-by-character, make sure the character is a digit, and keep track of the biggest digit seen so far.

#include<iostream>
#include <cctype> // needed for isdigit
//using namespace std; Not recommended. Causes problems

int main() 
{
    std::string num;
    char max = 0;
    std::cin >> num; // read number as a string.
    for (char ch: num)
    { //iterate string character by character
        if (!isdigit(static_cast<unsigned char>(ch)))
        { // if we didn't get a digit, the user screwed up (or is a jerk)
          // Let's not assume malice and let them know they've made a mistake.
            std::cerr << "Must input a valid number";
            return -1;
        }
        if (ch > max)
        { // this is the biggest character seen so far.
            max = ch; // update biggest
        }
    }
    std::cout << max; // print biggest
    return 0;
}

The answer of MBo is the best and should be accepted.

You are obviously not allowed to use C++ algorithms yet. And, maybe you are learing now about interger and modulo divisions.

If you would be allowed to use more advanced C++, you would probably write something like:

#include <iostream>
#include <algorithm>

int main()
{
    if (std::string number{}; (std::cin >> number) and std::all_of(number.begin(), number.end(), ::isdigit))
        std::cout << *max_element(number.begin(), number.end()) << '\n';
}

Below is the working example without using loops and without using std::max. Note this(without loop) is just one among many possible ways of doing it.

#include <iostream>
int findDigit(int passed_num, int current_num)
{
    
    int lastDigit;
  
    
    if (passed_num == 0) {
     
        return current_num;
    }
 
    // find the last didit
    lastDigit = passed_num % 10;

    if(lastDigit > current_num)
    {
      
         current_num = lastDigit;
    }
    
  
         //call findDigit() repeatedly
        current_num = findDigit(passed_num / 10, current_num);
  
        std::cout<<lastDigit<<" ";
    
    return current_num;
}
  
int main()
{
    std::cout << "Enter a number: ";
    int input_num, greatest_num;
    std::cin>>input_num;
    
    greatest_num = findDigit(input_num, 0);
    
    std::cout<<"greatest is: "<<greatest_num<<std::endl;
    
    std::cout<<"Enter another number: ";
    std::cin>>input_num;
    
    greatest_num = findDigit(input_num, 0);
    
    std::cout<<"greatest is: "<<greatest_num<<std::endl;
    
    return 0;
}

The output of the above is as follows:

Enter a number: 24344357
2 4 3 4 4 3 5 7 greatest is: 7
Enter another number: 2353639
2 3 5 3 6 3 9 greatest is: 9
#include <iostream>
using namespace std;
 
int main() {
   int a = 11;
   int b = 5;
   int c = 23;
 
   int max = (a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c) ;
    
   cout << max << endl;
}//using Ternary Operator
Related