Sunday, August 29, 2010

Print an Integer by using only putchar


#include<stdio.h>
main(int argc, char **argv)
{
int i, n, rev, lz = 0;
if(argc > 1) {
n = atoi(argv[1]);
} else {
printf("Enter the number : ");
scanf("%d", &n);
}
rev = 0;
lz = 0;
while(n != 0) {
if(rev == 0 && n%10 == 0) {
lz++;
}
rev = rev*10 + n%10;
n = n/10;
}
while(rev != 0) {
putchar(rev%10 + '0');
rev = rev/10;
}
while(lz--) {
printf("0");
}
printf("\n");
}

No comments:

Post a Comment