
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find the smallest digit in a given number
Given a non-negative number, the task is to find its smallest digit.
For example
Input:
N = 154870
Output:
0
Explanation: In the given number '154870', the smallest digit is '0'.
Approach to Solve this Problem
The simplest approach to solve this problem is to extract the last digit in the given number using the remainder theorem. While traversing the number, we will check if the extracted digit is less than the last digit, then return the output.
- Take a number n as the input.
- An integer function smallest_digit(int n) takes 'n' as the input and returns the smallest digit in the given number.
- Now initialize min as the last digit of the given number.
- Iterate through the number and check if the number extracted is less than the minimum number. If true, then update the minimum number and return the output.
- Remove the last digit by n/10 and check if there is another digit which is lesser than the current digit.
- Return the output.
Example
#include <iostream> using namespace std; int smallest_digit(int n) { int min = n % 10; //assume that last digit is the smallest n /= 10; //to start from the second last digit while (n != 0) { if (min > n % 10) min = n % 10; n /= 10; } return min; } int main() { int n = 154870; cout << smallest_digit(n); return 0; }
Running the above code will generate the output as,
Output
0
Explanation: In the given number '154870', the smallest digit is '0'.
- Related Articles
- Program to find nth smallest number from a given matrix in Python
- Find the difference between the largest 3 digit number and the smallest 6 digit number.
- Find the sum of the largest 5 -digit number and the smallest 6 -digit number.
- Find the difference between the largest 8-digit number and the smallest 6-digit number.
- JavaScript - Find the smallest n digit number or greater
- How To Find The Smallest 5 Digit Number Which Is A Perfect Square ?
- Python program to find the smallest number in a list
- 8085 Program to find the smallest number
- C++ program to find number in given range where each digit is distinct
- Python Program for Smallest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X?
- Java Program for Smallest K digit number divisible by X
- Program to reduce list by given operation and find smallest remaining number in Python
- Program to find super digit of a number in Python
- Java program to find the smallest number in an array

Advertisements