C++ Program to find the largest digit of a number


In this program, You will learn how to find largest digit of a number in C++.


The number is: 1234

Largest digit is: 4

Example: How to find the largest digit of a number in C++.

#include<iostream>
using namespace std;

int main() {

    int a, r, largest = 0;

    cout << "Enter a number:";
    cin>>a;

    while (a > 0) {
        r = a % 10;
        if (largest < r) {
            largest = r;
        }
        a = a / 10;
    }
    cout << "Largest digit is:" << largest;

    return 0;
}

Output:

Enter a number:23412
Largest digit is:4