Hi there ! I have a problem. I need to find the minimum value digit from the entered number. I've made a program that inputs a three digit number from the user and tells it's smallest digit. But the issue is I need it in a way that it can take any digit number (2,3,4 or so on) and tell the smallest digit in it.
Being a beginner I am only familiar with loops,if statements and switch statements. I can't use arrays ,strings etc in my program.
It goes like this:
#include <iostream>
int main()
{
int number ;
std::cout << "enter a positive number: " ;
std::cin >> number ;
if( number > 0 ) // if it is a positive number
{
int smallest_digit = 9 ;
while( number > 0 ) // as long as there is at least one digit left
{
constint last_digit = number % 10 ; // get the last digit
if( last_digit < smallest_digit ) smallest_digit = last_digit ;
number /= 10 ; // throw the last digit away, repeat with the remaining digits
}
std::cout << "smallest digit is: " << smallest_digit << '\n' ;
}
}
I need to initialise it with a large value (a value greater than or equal to the largest possible value of a digit).
This would have worked equally well: int smallest_digit = 5672 ;