I have program that find the smallest digit in umber, is just the result comes out as many times as do while loop works, how can i make output work just once?
#include <iostream>
usingnamespace std;
int main()
{
int a, digit, min=0, max=9;
cout<<"write any positive number!"<<endl;
cin>>a;
do{
digit=a%10; a=a/10;
}while (a!=0);
for (int i=min; i<=max; i++)
{
if (digit<i?i:digit) cout<<"-->"<<digit<<endl;
}
system ("pause");
return 0;
}
Put the code you need help with here.
You never change min nor max, thus the same output is looped through 10 times. You also never assigned the smallest digit, in your case, it will always be the last digit in the number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int a, digit, smallest = 9, largest = 0;
std::cout << "write any positive integer!" << std::endl;
std::cin >> a;
do
{
digit = a % 10;
if (digit < smallest)
smallest = digit;
if (digit > largest)
largest = digit;
a /= 10;
}
while (a != 0);
std::cout
<< "smallest: " << smallest << std::endl
<< "largest: " << largest << std::endl;