Given only putchar (no sprintf, itoa, etc.) write a routine
putlong that prints out an unsigned long in decimal.




Answers were Sorted based on User's Feedback



Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned lon..

Answer / raghuram.a

#include <stdio.h>
#include<conio.h>
void putlong(unsigned long x)
{
if (x>=10)
{
putlong(x/10);
}
putchar(x%10+48);
}
main()
{
unsigned long a;
clrscr();
printf("enter long integer:");
scanf("%ld",&a);
putlong(a);
getch();
return 0;

}

Is This Answer Correct ?    12 Yes 0 No

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned lon..

Answer / prateek caire

void print(unsigned n)
{
static c = 0;
if( n < 10)
{
putchar(n + 48);
return;
}
int m = n%10;
print(n/10);
if(++c%3 == 0) putchar(',');
putchar(48 + m);
}

Is This Answer Correct ?    7 Yes 2 No

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned lon..

Answer / suraj gupta

#include<stdio.h>
#include<string.h>
void printInt(unsigned long x){
int div=1;
if (x > 10){
div=x/10;
printInt(div);
}
//putchar(x % 10+'0');Both will work fine
putchar(x % 10+48);
}
int main(){
long int a;
printf(" Enter integer value : ");
scanf("%d",&a);
printf("\n");
printInt(a);
return 0;
}

Is This Answer Correct ?    1 Yes 1 No




Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned lon..

Answer / sanjay

#include<stdio.h>
#include<conio.h>

int main()
{
unsigned long abc=234455787; int div1;
div1=print_int(abc);
getch();
}


int print_int(long val)
{ int div=1; char c;
if (val>=10)
{
div=print_int(val/10);
}
putchar(val%10+48);
return (1);
}

Is This Answer Correct ?    0 Yes 0 No

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned lon..

Answer / meibella

void putLong(unsigned int x, unsigned int *a, int *j)
{
unsigned int temp = x/10;
unsigned int remainder = x%10;
a[(*j)++] = remainder;
if (temp < 1)
return ;
else
putLong(temp,a,j);


}
int main()
{
unsigned int * a = new unsigned int[12] ;
int j =0;
putLong(1735648,a,&j);
for (int i = j-1; i>=0; i--)
{
putchar(a[i]+48);
}
return 0;
}

Is This Answer Correct ?    0 Yes 2 No

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned lon..

Answer / rakesh

void main()
{
unsigned long n;
int i;
void putlong(unsigned long);
cout<<"\nEnter the number::";
cin>>n;
putlong(n);
}

void putlong(unsigned long n)
{
if(n==0)
return;
print(n/10);
putchar((n%10) + 48);

}

Is This Answer Correct ?    2 Yes 10 No

Post New Answer



More C Code Interview Questions

Sorting entire link list using selection sort and insertion sort and calculating their time complexity

1 Answers   Microsoft, Infosys, NetApp,


void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }

1 Answers  


find A^B using Recursive function

2 Answers  


struct Foo { char *pName; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); strcpy(obj->pName,"Your Name"); printf("%s", obj->pName); } a. Your Name b. compile error c. Name d. Runtime error

3 Answers   HCL,


main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }

1 Answers  






What is "far" and "near" pointers in "c"...?

3 Answers  


main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

2 Answers  


main() { char not; not=!2; printf("%d",not); }

1 Answers  


Is the following code legal? struct a { int x; struct a b; }

1 Answers  


main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }

1 Answers  


What are the files which are automatically opened when a C file is executed?

1 Answers  


how to check whether a linked list is circular.

11 Answers   Microsoft,






Categories