Print a long int in C using putchar() only Last Updated : 21 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Write a C function print(n) that takes a long int number n as argument, and prints it on console. The only allowed library function is putchar(), no other function like itoa() or printf() is allowed. Use of loops is also not allowed. We strongly recommend to minimize the browser and try this yourself first. This is a simple trick question. Since putchar() prints a character, we need to call putchar() for all digits. Recursion can always be used to replace iteration, so using recursion we can print all digits one by one. Now the question is putchar() prints a character, how to print digits using putchar(). We need to convert every digit to its corresponding ASCII value, this can be done by using ASCII value of '0'. Following is complete C program. C /* C program to print a long int number using putchar() only*/ #include <stdio.h> void print(long n) { // If number is smaller than 0, put a - sign // and change number to positive if (n < 0) { putchar('-'); n = -n; } // Remove the last digit and recur if (n/10) print(n/10); // Print the last digit putchar(n%10 + '0'); } // Driver program to test above function int main() { long int n = 12045; print(n); return 0; } Output: 12045 Time Complexity: O(logn) Auxiliary Space: O(logn) One important thing to note is the sequence of putchar() and recursive call print(n/10). Since the digits should be printed left to right, the recursive call must appear before putchar() (The rightmost digit should be printed at the end, all other digits must be printed before it). Comment More infoAdvertise with us Next Article Print a long int in C using putchar() only K kartik Improve Article Tags : C Language C-String C-Library Similar Reads Print an Integer Value in C Printing an Integer in C is one of the most basic tasks. Input and output of the elements are essential for taking input from the user and then giving the output to the user. Here we are going to take an integer as input from the user with the help of the scanf() function and print that integer with 2 min read putchar() function in C The putchar(int ch) method in C is used to write a character, of unsigned char type, to stdout. This character is passed as the parameter to this method. Syntax: int putchar(int ch) Parameters: This method accepts a mandatory parameter ch which is the character to be written to stdout. Return Value: 1 min read Convert a floating point number to string in C Write a C function ftoa() that converts a given floating-point number or a double to a string. Use of standard library functions for direct conversion is not allowed. The following is prototype of ftoa(). The article provides insight of conversion of C double to string. ftoa(n, res, afterpoint) n -- 3 min read puts() vs printf() for printing a string In C, both puts() and printf() functions are used for printing a string on the console and are defined in <stdio.h> header file. In this article, we will discuss the differences in the usage and behavior of both functions. puts() FunctionThe puts() function is used to write a string to the con 3 min read How to write long strings in Multi-lines C/C++? Image a situation where we want to use or print a long long string in C or C++, how to do this? In C/C++, we can break a string at any point in the middle using two double quotes in the middle. Below is a simple example to demonstrate the same. C #include<stdio.h> int main() { // We can put tw 2 min read Like