Home > database >  Finding the Largest Digit in C
Finding the Largest Digit in C

Time:10-06

  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;
}

CodePudding user response:

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;

CodePudding user response:

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;
}

CodePudding user response:

Below is the working example without using loops and without using std::max.

#include <iostream>
int findDigit(int passed_num)
{
    
    static int localGreatest = 0;
    
    int lastDigit;
  
    
    if (passed_num == 0) {
     
        return localGreatest;
    }
 
    // find the last didit
    lastDigit = passed_num % 10;
    
    if(localGreatest < lastDigit)
    {
      localGreatest = lastDigit;  
    }
    
  
   //call findDigit() repeatedly
    findDigit(passed_num / 10);
  
    std::cout<<lastDigit<<" ";
    
    return localGreatest;
}
  
int main()
{
    std::cout << "Enter a number: ";
    int input_num, greatest_num;
    std::cin>>input_num;
    
    greatest_num = findDigit(input_num);
    
    std::cout<<"greatest is: "<<greatest_num<<std::endl;
    
    return 0;
}

In the out the program asks for a number from the user. And then shows the individual digits and prints the greatest among them. The output for some arbitrary int looks like follows:

Enter a number: 233224328
2 3 3 2 2 4 3 2 8 greatest is: 8

Also if the number gets too big to fit into int use long int or unsigned long int or whatever fits your need.

CodePudding user response:

 #include <iostream>
using namespace std;

int main() {
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if (n1 >= n2) {
        if (n1 >= n3)
            cout << "Largest number: " << n1;
        else
            cout << "Largest number: " << n3;
    }
    else {
        if (n2 >= n3)
            cout << "Largest number: " << n2;
        else
            cout << "Largest number: " << n3;
    }

    return 0;
}

INPUT: 173 OUTPUT: 7

CodePudding user response:

#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