find a minimum value digit in a number using a loop.

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include<iostream>
using namespace std;
int main()
{
	int number,divisor,remainder,answer,count,digit1,digit2,digit3;
	 
    	cout<<"Enter number:";
	cin>>number;

	for(divisor=100,answer=1,remainder=1,count=1; divisor>=1; divisor=divisor/10,count++)
	{
		answer=number/divisor;
		remainder=number%divisor;
		number=remainder;
		
		if(count==1)
		{
		digit1=answer;
		cout<<"Digit1:"<<digit1<<endl;
		}
		if(count==2)
		{
		digit2=answer;
		cout<<"Digit2:"<<digit2<<endl;
		}if(count==3)
		{
		digit3=answer;
		cout<<"Digit3:"<<digit3<<endl;
	}
	}
	
	
	if(digit1<digit2 && digit1<digit3)
	cout<<"First digit is smallest."<<endl;
	else if(digit2<digit1 && digit2<digit3)
	cout<<"Second digit is smallest."<<endl;
	else if(digit3<digit1 && digit3<digit2)
	cout<<"Third digit is smallest."<<endl;
	
}
The simplest way to do this is with a string. Why can't you use a string?
Last edited on
Because I have not learnt strings yet. I just know it by name.
Get input as a string.
Look at each character in the string, using a loop.
Remember which is the smallest as you go through them.
If there is any other way of doing it kindly tell.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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
        {
            const int 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' ;
    }
}
Thanks! it runs all right. But I have a couple of questions. Why you've taken smallest_digit as 9?
Why you have taken last_digit as a constant?
JLB's answer is very solid. Just for fun, here's another answer using a bit more of C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string number ;
    std::cout << "enter a positive number: " ;
    std::cin >> number ;

    std::cout << "Min is: " << *min_element(number.begin(), number.end());
}
> Why you've taken smallest_digit as 9?

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 ;


> Why you have taken last_digit as a constant?

Force of habit. See: http://www.cplusplus.com/forum/beginner/180986/#msg888218



1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
    std::string number ;
    std::cout << "enter a positive number: " ;
    std::cin >> number ;

    if( !number.empty() && std::all_of( number.begin(), number.end(), []( char c ) { return std::isdigit(c) ; } ) )
        std::cout << "smallest digit is: " << *std::min_element( number.begin(), number.end() );
}

Thanks all !
Topic archived. No new replies allowed.