Given a number N, the task is to write a C program to print all digits of the number N in their original order.
Examples:
Input: N = 12
Output: 1, 2Input: N = 1032
Output: 1, 0, 3, 2
Method 1: The simplest way to do is to extract the digits one by one and print it.
- Extract the last digit of the number N by N%10, and store that digit in an array(say arr[]).
- Update the value of N by N/10 and repeat the above step till N is not equals to 0.
- When all the digits have been extracted and stored, traverse the array from the end and print the digits stored in it.
Below is the implementation of the above approach:
// C program of the above approach #include <stdio.h> #define MAX 100 // Function to print the digit of // number N void printDigit(int N) { // To store the digit // of the number N int arr[MAX]; int i = 0; int j, r; // Till N becomes 0 while (N != 0) { // Extract the last digit of N r = N % 10; // Put the digit in arr[] arr[i] = r; i++; // Update N to N/10 to extract // next last digit N = N / 10; } // Print the digit of N by traversing // arr[] reverse for (j = i - 1; j > -1; j--) { printf("%d ", arr[j]); } } // Driver Code int main() { int N = 3452897; printDigit(N); return 0; } |
3 4 5 2 8 9 7
Time Complexity: O(log10 N)
Auxiliary Space: O(log10 N)
Method 2: Using Recursion
- Recursively iterate till N become 0:
- Base Case: If the value of N is 0, exit from the function.
if(N==0) return ;
- Recursive Call: If the base case is not met, recursively call for next iteration by updating N to N/10 and print the value of last digit extracted(say r) from the number N, after recursive call.
recursive_function(N/10); print(r);
- Base Case: If the value of N is 0, exit from the function.
Below is the implementation of the above approach:
// C program of the above approach #include <stdio.h> // Function to print the digit of // number N void printDigit(int N) { int r; // Base Case if (N == 0) { return; } // Extract the last digit r = N % 10; // Recursive call to next // iteration printDigit(N / 10); // Print r printf("%d ", r); } // Driver Code int main() { int N = 3452897; printDigit(N); return 0; } |
3 4 5 2 8 9 7
Time Complexity: O(log10 N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print a number strictly less than a given number such that all its digits are distinct.
- Print a number containing K digits with digital root D
- C Program for Sum the digits of a given number
- Program for Sum of the digits of a given number
- C program to Count the digits of a number
- Program to calculate product of digits of a number
- C Program to Check if all digits of a number divide it
- Program to check if a number is divisible by any of its digits
- Program to find last two digits of Nth Fibonacci number
- Write a program to reverse digits of a number
- Program to check if a number is divisible by sum of its digits
- Program to check if a given number is Lucky (all digits are different)
- Program to insert dashes between two adjacent odd digits in given Number
- C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Program to print number pattern
- Program to print all the numbers divisible by 3 and 5 for a given number
- Program to print 'N' alphabet using the number pattern from 1 to n
- C Program to Print Number series without using any loop
- C program to print number of days in a month
- Program to print multiplication table of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

