ASCII math in C

758 Views Asked by At

In C i can't simply use putchar(5);

I have to do it like this

#include <stdio.h>



    int main(){
        int c=5;




     putchar(c+'0');


    putchar('\n');


    return 0;

    }

the output is 5.

But I can't do this for double digit numbers like 10 or 25. My question is , is it possible to print out 10 using putchar and putchar only?

I can't do it like this

 int c=10;
 putchar(c+'0');

The output would be':' because ':' has the ASCII value of 58.

1

There are 1 best solutions below

0
On

If it is only numbers you can try the below logic.

#include <stdio.h>
int main()
{
    int c = 465, n = 0, i = 1;

    while(c > 0){
        n *= (10 * i);
        n += (c % 10);
        c /= 10;
    }

    do {
        putchar((n % 10)+'0');
        n /= 10;
    } while(n > 0);

    putchar('\n');
    return 0;
}