C Program to find largest and smallest digit of a number


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


1234 => Largest & Smallest digit is: 4 1


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

Run it Now
#include<stdio.h>

int main() {

   int n, r, sml = 9, lar = 0;

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

   while (n > 0) {
       r = n % 10;
       if (sml > r) {
           sml = r;
       }
       if (lar < r) {
           lar = r;
       }
       n = n / 10;
   }
   printf("\nThe Largest Digit is :%d", lar);
   printf("\nThe Smallest Digit is :%d", sml);

   return 0;
}

Output:

Enter a Number :9645
The Largest Digit is :9
The Smallest Digit is :4