[algogeeks] Re: use putchar to print out an unsigned long in decimal

2009-10-15 Thread umesh kewat
Thanks Debanjan for intimating the bug in my code and Sorry i forget to add 48 in second putchar statement. now rite code is #include void print(unsigned long int n) { if(n>9) print(n/10); putchar('0'+(n%10)); } int main() {

[algogeeks] Re: use putchar to print out an unsigned long in decimal

2009-10-15 Thread Lego Haryanto
Logic is okay, but the second putchar() call should be offset-ed by '0' character, not just 0-9 ... I'd also avoid using hardcoded 48, not every compiler operates on ASCII literal (e.g, EBCDIC on some IBM mainframes). Best, -Lego On Thu, Oct 15, 2009 at 11:38 AM, Debanjan wrote: > > On Oct 15,

[algogeeks] Re: use putchar to print out an unsigned long in decimal

2009-10-15 Thread Debanjan
On Oct 15, 10:45 am, umesh kewat wrote: > Hi, > Here is the code for problem... > > void print(unsigned long int n) > { >     if(n<10) > >         putchar(n+48); >     else >         { >         print(n/10); >         putchar(n%10); >         } > > } > > int main() > { >     unsigned long int n;

[algogeeks] Re: use putchar to print out an unsigned long in decimal

2009-10-15 Thread Lego Haryanto
void print(unsigned long x) { if (x >= 10) print(x / 10); putchar('0' + (x % 10)); } On Wed, Oct 14, 2009 at 10:45 PM, umesh kewat wrote: > Hi, > Here is the code for problem... > > void print(unsigned long int n) > { > if(n<10) > > putchar(n+48); > else > { >

[algogeeks] Re: use putchar to print out an unsigned long in decimal

2009-10-15 Thread umesh kewat
Hi, Here is the code for problem... void print(unsigned long int n) { if(n<10) putchar(n+48); else { print(n/10); putchar(n%10); } } int main() { unsigned long int n; scanf("%ld",&n); print(n); return 0; } On Thu, Oct 15, 2009 at 9

[algogeeks] Re: use putchar to print out an unsigned long in decimal

2009-10-15 Thread Debanjan
On Oct 15, 9:03 am, ankur aggarwal wrote: > 1.      Given only putchar (no sprintf, itoa, etc.) write a routine putlong > that prints out an unsigned long in decimal. Well,this is rather very easy : void putlong(unsigned long long num) { if(num > 9) putlong(num/10); putchar(num%1

[algogeeks] Re: use putchar to print out an unsigned long in decimal

2009-10-15 Thread umesh kewat
no need to take care of negative numbers because given number is unsigned long integer .. apply simple algo :) On Thu, Oct 15, 2009 at 1:07 PM, Siddharth S wrote: > > void putlong(long n) > { >if(n == 0) // special case >{ > putchar('0'); > return; >} >else >i

[algogeeks] Re: use putchar to print out an unsigned long in decimal

2009-10-15 Thread Siddharth S
void putlong(long n) { if(n == 0) // special case { putchar('0'); return; } else if(n < 0) // if number is negative, print "-" sign and continue as if it is a positive number putchar('-'), n = -n; int arr[30]; int arr_cnt = 0; while(n) { arr[arr_cnt+