C Program to find the largest and smallest digit of a number

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


while(condition){
    //statement/
    increment/decrement
}

C Program to find the largest and smallest digit of a number

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

#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("Largest digit is:%d", lar);
   printf("\nSmallest digit is:%d", sml);

   return 0;
}

Output:

Enter a number:23423                                                                                                                   
Largest digit is:4                                                                                                                     
Smallest digit is:2