0

Please help find me error

void print(int k) { if (k<0) { putchar('-'); k=-k; } if(k/10) { print(k/10); putchar(k%10); } } int main() { int n; n=1920; print(n); return 0; }

15th Apr 2018, 10:46 AM
Sufal Chhabra
Sufal Chhabra - avatar
2 Answers
+ 2
Sufal Chhabra when you use the putchar function to print an integer from 0 to 9 you have to add 48 or hexadecimal 0x30 to the number to get the correct ascii code for the number. I added an else to the second if in order to print the number when it is smaller than 10. here is the code with the changes: https://code.sololearn.com/cEZ0YzBpHrqA/?ref=app
15th Apr 2018, 11:38 AM
Ulisses Cruz
Ulisses Cruz - avatar
0
the code is supposed to print digits of the number as a character string
15th Apr 2018, 11:08 AM
Sufal Chhabra
Sufal Chhabra - avatar