I've got a string of n numbers,introduced from the keyboard.For every number in the string that contains 0 I am asked to display it without 0.Example:
15 543 3001 601 -------> 15 543 31 61
#include <iostream>
int main()
{
std::cout << "how many numbers? " ;
int cnt ;
std::cin >> cnt ;
for( int i = 0 ; i < cnt ; ++i )
{
std::cout << "number? " ;
unsignedint number ;
std::cin >> number ;
// get the number stripped of zeroes, with digits in reverse order
// (this is a loop loop exactly like the one in your original code)
unsignedint no_zeroes_reversed = 0 ;
while( number != 0 )
{
constint digit = number%10 ;
if( digit != 0 ) no_zeroes_reversed = no_zeroes_reversed * 10 + digit ;
number /= 10 ;
}
// run the another similar loop to reverse the digits of no_zeroes_reversed
unsignedint no_zeroes = 0 ;
while( no_zeroes_reversed != 0 )
{
no_zeroes = no_zeroes * 10 + no_zeroes_reversed%10 ;
no_zeroes_reversed /= 10 ;
}
if( no_zeroes != 0 ) std::cout << no_zeroes << '\n' ;
}
}