In the previous program of number we have learned how to extract digit from a number entered by the user.In this program we are going to find out largest and smallest digit in the number.Think how you can do this?It can be done by comparing each digit of particular number.For this previous concept of extracting digit from number will be used.Code written below can be used to find out largest and smallest digit from number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/*This program takes an integer from user and finds out the largest digit in the number*/ #include<stdio.h> int main() { int n,p,i=0,digit,large=0;//variables printf("enter any integer number:\n"); scanf("%d",&n);//taking input p=n;//storing original value entered by user to variable p while(n>0) { digit=n%10;//finding digit if(digit>large)//checking condition for large large=digit; n=n/10;//dividing number by 10 } printf("Largest digit in number %d=%d",p,large);//displaying the largest number return 0; } |

Program compiled in Dev C++ compiler.

**OUTPUT:**

1 2 3 |
enter any integer number: 7865 Largest digit in number 7865=8 |

**Description:**

- In the above program first I have taken an integer number as input from user.
- Assume largest digit will be 0.
- Stored the number entered by the user in variable p to use the original value later in the program entered by the user.
- In while loop I am finding the each digit using % operator (modulo operator) and after getting digit comparing it with largest digit stored at variable large,if digit is greater than value at variable large replace the value at large with the new digit obtained.
- Next dividing the number by 10 so that after getting first digit it will be removed from original number.
- Loop will be continue upto number > 0.
- Finally largest digit will be obtained and display the result.

This program finds the smallest digit in number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/*This program takes an integer as input from user and finds out the smallest digit from that number*/ #include<stdio.h> int main() { int n,p,i=0,digit,small=9;//variables printf("enter any integer number:\n"); scanf("%d",&n);//taking input p=n;//storing original value entered by user to variable p while(n>0) { digit=n%10;//finding digit if(digit==0) { small=0;//assigning 0 to small } if(digit<=small) { small=digit;//assigning digit to small } n=n/10;//dividing number by 10 } printf("Smallest digit in number %d=%d",p,small);//displaying the largest number return 0; } |

Program compiled in Dev C++ compiler.

**OUTPUT:**

1 2 3 |
enter any integer number: 76545412 Smallest digit in number 76545412=1 |

**Description:**

- In the above program first I have taken an integer number as input from user.
- Assume smallest digit will be 9.
- Stored the number entered by the user in variable p to use the original value later in the program entered by the user.
- In while loop I am finding the each digit using % operator (modulo operator) and after getting digit comparing it with smallest digit stored at variable small,if digit is small than value at variable small replace the value at small with the new digit obtained.
- Next dividing the number by 10 so that after getting first digit it will be removed from original number.
- Loop will be continue up to number > 0.
- Finally smallest digit will be obtained and display the result.

### References:”C Programming languages” By D.M. Ritchie

### Click here to appear free online test powered by Quick Solution.

### Some related programs.

- C program to find Armstrong number.
- C program to check perfect number.
- C program to finds digits in number.
- C program to count digits and vowels in number.
- C program to count words in array.
- C program to search words in array.

## Click here to go Program Gallery.

## One thought on “C program to find largest and smallest digit in number”