Print an integer using only putchar. Try doing it without
using extra storage.




Answers were Sorted based on User's Feedback



Print an integer using only putchar. Try doing it without using extra storage...

Answer / raghuram

void putlong(unsigned long x)
{ if (x > 10) putlong(x / 10);
putchar(x % 10+'0');
}
main()
{
long int a;
printf("enter long integer:");
scanf("%d",&a);
putlong(a);
return 0;

}

Is This Answer Correct ?    22 Yes 5 No

Print an integer using only putchar. Try doing it without using extra storage...

Answer / cmos

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);
}

Is This Answer Correct ?    2 Yes 4 No

Post New Answer




More C Code Interview Questions

main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }

2 Answers  


# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

1 Answers  


main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\nā€", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above

3 Answers   HCL,


main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


Develop a routine to reflect an object about an arbitrarily selected plane

0 Answers  






Write a c program to search an element in an array using recursion

1 Answers   Wipro,


void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }

1 Answers   Honeywell,


Write a program that find and print how many odd numbers in a binary tree

1 Answers  


void ( * abc( int, void ( *def) () ) ) ();

1 Answers  


Sir... please give some important coding questions asked by product companies..

0 Answers  


main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }

2 Answers   Wipro,


Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.

2 Answers   Wipro,






Categories