How do we convert a number with leading zeros to a number? For example: if the input is 004, the output should be 400.
I wrote the program below, but it only works when there are no leading zeros in the input.
int num;
cout<<"Enter number "<<endl;
cin>>num;
int rev = 0;
int reminder;
while(num != 0)
{
reminder = num % 10;
rev = rev * 10 + reminder;
num = num / 10;
}
cout<<"Reverse = "<<rev<<endl;
Is there a way to enter a number with leading zeros? Even then, the above logic does not work for such numbers.
Any simple solution? This is doable by accepting the input as a string and processing it. But it does not look beautiful.
* EDIT: if the length of the number is known, it can be turned to a number with zero values. (without using a string) *
I will send the code as soon as it works.
EDIT 2: I tried to return the characters to the cin stream and then read and calculate the opposite. It works for two-digit numbers.
, . , , 10 .
, .
, :)