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


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


Number is : 1234 = 3


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

Run it Now
#include<iostream>
using namespace std;

int main() {

   int n, num, rev;
   int r, lar = 0, sl = 0;

   cout << "Enter a Number :";
   cin>>n;

   while (n > 0) {
       r = n % 10;
       if (lar < r) {
           lar = r;
       }
       rev = rev * 10 + r;
       n = n / 10;
   }

   n = rev;
   while (n > 0) {
       r = n % 10;
       if (lar != r) {
           if (sl < r) {
               sl = r;
           }
       }
       n = n / 10;
   }
   cout << "\nSecond Largest Digit :=" << sl;

   return 0;
}

Output:

Enter a Number :62935
Second Largest Digit :=6