C++ Program to find largest digit of a number


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


Number is : 1234

Largest digit is : 4


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

Run it Now
#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 << "\nLargest Digit is :" << largest;

    return 0;
}

Output:

Enter a Number :2351
Largest Digit is :5