C program to find largest and smallest digit in number

largest and smallest digit

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.

Program compiled in Dev C++ compiler.

OUTPUT:

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.

Program compiled in Dev C++ compiler.

OUTPUT:

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.

Click here to go Program Gallery.



           







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

  1. Pingback: marseille shirt

Leave a Reply