C++ Program to find the smallest digit of a number


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


The number is: 1234

Smallest digit is: 1

Example: How to find the smallest digit of a number in C++.

#include<iostream>
using namespace std;

int main() {

  int a, r, smallest = 9;

  cout << "Enter a number:";
  cin>>a;

  while (a > 0) {
      r = a % 10;
      if (smallest > r) {
          smallest = r;
      }
      a = a / 10;
  }
  cout << "Smallest digit is:" << smallest;

  return 0;
}

Output:

Enter a number:4356
Smallest digit is:3