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.


1234 => Second largest digit is: 3


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

Run it Now
#include<stdio.h>

int main() {

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

   printf("Enter a Number :");
   scanf("%d", &n);

   while (n > 0) {
       r = n % 10;
       if (l < r) {
           l = r;
       }

       rev = rev * 10 + r;
       n = n / 10;
   }
   printf("\nFirst Largest Digit :%d", l);

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

   return 0;
}

Output:

Enter a Number :23856
First Largest Digit :8
Second Largest Digit :6