C Program to find the largest digit of a number


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


1234 => Largest digit is: 4

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

#include<stdio.h>

int main() {

   int num, r, ld = 0;

   printf("Enter a number:");
   scanf("%d", &num);

   while (num > 0) {
       r = num % 10;
       if (ld < r) {
           ld = r;
       }
       num = num / 10;
   }
   printf("Largest digit is:%d", ld);

   return 0;
}

Output:

Enter a number:3421                                                                                                                    
Largest digit is:4