Last Updated on April 13, 2023 by Prepbytes
Reversing a number is a useful task in programming. It can be used in various applications such as checking if a number is a palindrome or calculating the reverse of a sequence of digits. In C, reversing a number is a relatively simple task that can be accomplished with a few lines of code. In this blog, we will discuss to reverse a number in C in an iterative and recursive manner. We will see the algorithms and code implementation accordingly, If you are a programming beginner, this article will be for you. We will solve this problem in different ways.
Example:
Input: 6789
Output: 9876
Algorithm to C Program to Reverse a Number
- Take the input from the user
- Perform remainder = number % 10
- Print remainder
- Perform number/10
- Repeat steps 2 and 4 until the input number cannot be further divided.
Method 1: An Iterative Method to Reverse a Number in C
In this method, we will use a while loop to reverse a number.
Code Implementation to Reverse a Number in C by Iterative Method
#include <stdio.h> int main() { int number, rev = 0, remainder; printf("\nEnter a number: "); scanf("%d",&number); printf("\nReversed Number: "); while(number != 0) { remainder = number%10; rev = rev*10 + remainder; number /= 10; } printf("%d",rev); return 0; }
Output:
Enter a number: 6789
Reversed number: 9876
Time Complexity of reverse a number in C Language: O(log(N))
Method 2: Recursive Program to Reverse a Number in C
In this method, we will use a recursive function to apply the reverse number in which the function calls itself from its definition part.
Algorithm to C Program to Reverse a Number Recursively
- Create a reverse(int n), a recursive function of void type.
- The base condition will be: if (n <10), then print(n) and return.
- Otherwise, print(n%10) and call function reverse(n/10).
Code Implementation to Reverse a Number in C Recursively
#include <stdio.h> int reverse_digits(int num) { static int rev_num = 0; static int base_pos = 1; if(num > 0) { reverse_digits(num/10); rev_num += (num%10)*base_pos; base_pos *= 10; } return rev_num; } int main() { int num; printf("\nEnter a number: "); scanf("%d",&num); printf("\nReversed number: %d",reverse_digits(num)); return 0; }
Output:
Enter a number: 6789
Reversed number: 9876
Conclusion
In this article, we have discussed the most efficient and effective way for C program to reverse a number. we have discussed two methods with their respective algorithms and time complexities. We hope this blog will help you to clear your fundamentals. Also, you can practice more questions on our MYCODE platform. These questions are created by our experts so we hope this article will build your logic-building skills and enhance your programming skills.
Frequently Asked Questions
Here we have FAQs on reverse a number in C language
Q1: What does it mean to reverse a number?
Ans: Reversing a number means changing the order of its digits. For example, if the original number is 1234, its reverse would be 4321.
Q2: How does the code for reversing a number work?
Ans: The code uses a while loop to extract each digit of the input number, starting from the rightmost digit. Each extracted digit is then added to the reversed number, which is initially set to 0, and multiplied by 10. This effectively "shifts" the digits of the reversed number to the left, making room for the next extracted digit. After all, the digits have been extracted and added to the reversed number, the loop exits, and the reversed number is printed.
Q3: What happens if I enter a negative number?
Ans: The code will still work, but the resulting reversed number will also be negative. For example, if you enter -1234, the code will output -4321.
Q4: What happens if I enter a number with leading zeros?
Ans: The code will still work, but the leading zeros will be lost in the reversed number. For example, if you enter 00123, the code will output 321.
Q5: What is the maximum number that can be reversed using this code?
Ans: The maximum number that can be reversed depends on the size of the int data type in your system. In most systems, int is a 32-bit signed integer, which means the maximum number that can be stored in an int variable is 2^31 – 1 (or 2147483647 in decimal). If you try to reverse a number larger than this, the code will not work correctly.
Other C Programs
C program to calculate percentage of 5 subjects
C program to convert binary number to decimal number
C program to convert celsius to fahrenheit
C program to convert infix to postfix
C program to find area of circle
C program to find roots of quadratic equation
C program to add two numbers
C program for merge sort for linked lists
C program for performing bubble sort on linked list
C program to reverse a linked list