R 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 R.


Number :4567

Largest is: 7

Smallest is = 4

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

{
    n = as.integer(readline(prompt = "Enter a number :"))
    sm = 9
    largest = 0

    while (n > 0) {
      r = n %% 10
      if (sm > r) {
        sm = r
      }
      if (largest < r) {
        largest = r
      }
      n = n %/% 10
    }

    print(paste("Smallest digit is :", sm))
    print(paste("Largest digit is :", largest))
}

Output:

Enter a number :43162
[1] "Smallest digit is : 1"
[1] "Largest digit is : 6"