“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
The idea to get one by one each digit and then transfer it as a parameter to the putchar function. The easiest digit to get is the last digit because for a number n in decimal the last digit is n%10. To get the other digit get rid of this last digit by dividing n with 10 and go on using the same technique. As you can see the digits are obtained in reverse order but allocating some extra space to store and then print them is useless. Remember that a recursive function call provide to you the powerful stack mechanism so let use it.
void putlong( unsigned long l) { if (l / 10 != 0) /* Be sure that there is an exit from the recursive call */ putlong(l/10); /* Forget temporarly about last digit and print the rest of the number */ /* The digits before are already printed so let print the current digit; Don’t forget to convert the digit to ASCII */ putchar(‘0’+l%10); }
Note: The condition that the parameter to be unsigned make your life easier because you don’t have to bother about number sign. If the number is signed than you must take of the sign too. The alghoritm will be:
void putlong(long l) { if ( l / 10 == 0) /* this is the first digit so at the beginning put the sign; that will stop the recursive call*/ { if (l < 0) putchar(‘-‘); } else putlong(l/10); /* Forget temporarly about last digit and print the rest of the number */ /* The digits before are already printed so let print the current digit; Don’t forget to convert the digit to ASCII */ putchar(‘0’+abs(l%10)); /* if the number is negative the modulus is negative too */ }
I hope this would be the fastest solution comparing all above.
printnumber(int i);
{
int x = 10000; // assuming 2 byte int.
int y ;
while (x > 0)
{
putchar(’0′ + (i / x % 10));
x = x/10;
}
}
I have not tested this with complier but i hope this will work. Please send any feedback on n_k_goyal@yahoo.com
@Nitin:
Your program brings in a lot of extra ‘0′ prefixed to the original number.
@Others:
By using recursion you’re using the stack space and hence not using constant space.
A better way is to introduce another integer which holds the reverse of the input integer(of course, taking care of the last 0s, then print that reversed integer directly. It’s not difficult, is it?
int getCount(int i)
{
int count=0,rev=0;
while(i)
{
rev=rev*10+i%10;
i/=10;
count++;
}
while(count–)
{
i=i*10+rev%10;
rev/=10;
}
return i;
}
int(n) to char(c):
n = c + ‘0′ - ‘A’;
The idea to get one by one each digit and then transfer it as a parameter to the putchar function. The easiest digit to get is the last digit because for a number n in decimal the last digit is n%10. To get the other digit get rid of this last digit by dividing n with 10 and go on using the same technique. As you can see the digits are obtained in reverse order but allocating some extra space to store and then print them is useless. Remember that a recursive function call provide to you the powerful stack mechanism so let use it.
void putlong( unsigned long l)
{
if (l / 10 != 0) /* Be sure that there is an exit from the recursive call */
putlong(l/10); /* Forget temporarly about last digit and print the rest of the number */
/* The digits before are already printed so let print the current digit;
Don’t forget to convert the digit to ASCII */
putchar(‘0’+l%10);
}
Note: The condition that the parameter to be unsigned make your life easier because you don’t have to bother about number sign. If the number is signed than you must take of the sign too. The alghoritm will be:
void putlong(long l)
{
if ( l / 10 == 0) /* this is the first digit so at the beginning put the sign; that will stop the recursive call*/
{
if (l < 0)
putchar(‘-‘);
}
else
putlong(l/10); /* Forget temporarly about last digit and print the rest of the number */
/* The digits before are already printed so let print the current digit;
Don’t forget to convert the digit to ASCII */
putchar(‘0’+abs(l%10)); /* if the number is negative the modulus is negative too */
}
void printInt(int a);
int b = a;
char *str;
int i = 1;
int len = 0;
while (b) {
b /= 10;
i *= 10;
len++;
}
i /= 10;
while (i > 0) {
putchar(a/i + 48);
a = a%i;
i /= 10;
}
}
This can be done by recursion.
Since the number of recursive calls is not significant, it does not affect the performance much
printnumber(int i)
{
if(i == 0)
return;
printnumber(i/10);
putchar(’0′ + i%10);
}
void putlong(long l)
{
if (l == 0) return;
if (l < 0) { putchar(’-'); putlong(-1*l); return;}
putlong(l/10);
putchar(’0′ + l%10);
}
void putnm(int N)
{
int digits = (int)log10(N);
while(N)
{
register int ans = (int)(N / ( pow(10, digits) ));
putchar(’0′ + ans);
N -= (ans * (int)pow(10,digits) );
digits–;
}
}
int i;
putchar(i+’0′);
I hope this would be the fastest solution comparing all above.
printnumber(int i);
{
int x = 10000; // assuming 2 byte int.
int y ;
while (x > 0)
{
putchar(’0′ + (i / x % 10));
x = x/10;
}
}
I have not tested this with complier but i hope this will work. Please send any feedback on n_k_goyal@yahoo.com
@Nitin:
Your program brings in a lot of extra ‘0′ prefixed to the original number.
@Others:
By using recursion you’re using the stack space and hence not using constant space.
A better way is to introduce another integer which holds the reverse of the input integer(of course, taking care of the last 0s, then print that reversed integer directly. It’s not difficult, is it?
int getCount(int i)
{
int count=0,rev=0;
while(i)
{
rev=rev*10+i%10;
i/=10;
count++;
}
while(count–)
{
i=i*10+rev%10;
rev/=10;
}
return i;
}
You can’t say this is a constant space solution(you’re using 2 extra integers). But it is elegant enough.
Leave an Answer/Comment