Reverse a Number in C means moving the digit at the last position to the first position and vice versa.
For example, if the given number is “1234”, the reverse number will be “4321”.
There are several ways to reverse a number in C language. Let’s take a detailed look at all the approaches to reverse a number.
- Reverse a Number in C using While Loop
- Reverse a Number in C using For Loop
- Reverse a Number in C using Function
- Reverse a Number in C using Recursion
In this approach, we will divide the number by 10 and add the remainder to the sum.
Example:
The following C program uses a while loop to reverse the digits of the number and display it on the output of the terminal.
For example, 323441 becomes 144323
Here is source code of the C program to reverse a given number using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* C program to reverse a number */
#include <stdio.h>
int main(void)
{
long num, reverse = 0, temp, remainder;
printf("Enter the number:\n");
scanf("%ld", &num);
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Reverse of a Number is %ld\n", reverse);
}
1. Take the number which you have to reverse as the input and store it in the variable num.
2. Copy the input number to the another variable temp.
3. Firstly initialize the variable reverse to zero.
4. Upon receiving the number, the program divides the number by 10 and stores the remainder in a variable.
5. Then, it adds the remainder to the sum where sum multiplies its previous value by 10.
6. The program then repeats the process until the number is not 0.
7. When it becomes zero, print the given number and its reverse using variables temp and reverse respectively as output.
Time complexity: O(n)
The time complexity of this algorithm is O(n), where n is the number of digits in the number.
Space complexity: O(1)
Because no extra space is used to store the digit, the space complexity of this algorithm is O(1).
Testcase 1: In this case, we are entering the number “323441” as input.
Enter the number: 323441 Given number = 323441 Reverse of a Number is 144323
Testcase 2: In this case, we are entering the number “42394” as input.
Enter the number 42394 Given number = 42394 Reverse of a Number is 49324
Ask for the number as user input. In this case, as we are using for loop, it is essential to know the size of the number beforehand. Therefore we should make use of the log10 function to find the size of the number.
Methods used:
long reverse(long) – This function will reverse the number.
Approach:
1. Take the number as input.
2. Store the number in a variable.
3. Find the size of the number using log10() function.
4. Inside for loop, take the number and divide it by 10 and store the remainder in a variable.
5. Add the remainder to the sum where sum multiplies its previous value by 10.
6. Repeat the process until the number is not 0 until the loop ends.
7. Return the sum.
Here is source code of the C program to reverse a given number using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* C program to reverse a number using for loop */ #include <stdio.h> #include <math.h> long reverse(long num) { long reverse = 0; long temp; long remainder; long size = log10(num); for (int i = 0; i <= size; i++) { temp = num; remainder = temp % 10; reverse = reverse * 10 + remainder; num = num / 10; } return reverse; } int main(void) { long num; printf("Enter the number:\n"); scanf("%ld", &num); printf("The reverse of the number is %ld\n", reverse(num)); return 0; }
1. The program begins with asking the user for a number.
2. The number is stored in a variable num and then passed to the function reverse().
3. In reverse() function, we find the log of the number which is the size of the number and then iterate through the for loop from zero to size and use the basic division algorithm to find the reverse of the number.
4. print the given number and its reverse using variable reverse as output.
Time complexity: O(n)
The time complexity of this algorithm is O(n), where n is the number of digits in the number.
Space complexity: O(1)
Because no extra space is used to store the digit, the space complexity of this algorithm is O(1).
Testcase 1: In this case, we are entering “24981” as input to revere a number.
Enter the number: 24981 The reverse of the number is 18942
Testcase 2: In this case, we are entering “172398” as input to revere a number.
Enter the number: 172398 The reverse of the number is 893271
In this approach, we will use a separate function to calculate the reverse of the number.
Methods used:
int reverse(int n) – This function calculates the reverse of the number.
Approach for Reversing a Number in C using Function
1. Take the number as input.
2. Store the number in a variable.
3. Call the function reverse to calculate the reverse of the number.
4. Print the reverse.
Here is source code of the C program to reverse a given number using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* C program to reverse a number using function*/
#include <stdio.h>
int reverse(int n)
{
int rev = 0;
while (n > 0)
{
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Reversed number is %d", reverse(n));
return 0;
}
1. The program asks the user for a number.
2. Upon receiving the number, the program calls the function reverse to calculate the reverse of the number.
rev = rev * 10 + n % 10;
3. Then, it prints the reverse.
Time complexity: O(n)
The time complexity of this algorithm is O(n), where n is the number of digits in the number.
Space complexity: O(1)
Because no extra space is used to store the digit, the space complexity of this algorithm is O(1).
Testcase 1: In this case, we enter the number “453424123” as input.
Enter a number: 453424123 Reversed number is 321424354
Testcase 2: In this case, we enter the number “0324135” as input.
Enter the number: Enter a number: 0324135 Reversed number is 531423%
In this approach, we will use a recursive function to calculate the reverse of the number.
Methods used:
int reverse(int n) – This function calculates the reverse of the number using recursion.
Approach to Reverse a Number using Recursion in C
1. Take the number as input.
2. Store the number in a variable.
3. Call the function `reverse` to calculate the reverse of the number.
4. Make a termination case, here it is when the number is 0.
5. In all other cases, call the function `reverse` again with the number divided by 10.
6. Return the sum.
Here is source code of the C program to reverse a given number using recursive function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* C program to reverse a number using recursion */
#include <stdio.h>
int reverse(int n, int rev)
{
if (n == 0)
return rev;
rev = rev * 10 + n % 10;
return reverse(n / 10, rev);
}
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Reversed number is %d", reverse(n, 0));
return 0;
}
The program asks the user for a number. Upon receiving the number, the program calls the function reverse to calculate the reverse of the number. In the reverse function whenever the number becomes zero, the function returns the reverse of the number. Otherwise, the function calls itself recursively by dividing n by 10 at each step. It also stores the reverse of the number in a variable.
Time complexity: O(n)
The time complexity of this algorithm is O(n), where n is the number of digits in the number.
Space Complexity: O(n)
The space complexity of this algorithm is O(n), where n is the number of digits in the number. n here also becomes the stack size taken by the function.
Testcase 1: In this case, we are entering the number “13223” as input to revere a number.
Enter a number: 13223 Reversed number is 32231
Testcase 2: In this case, we are entering the number “90831” as input to revere a number.
Enter a number: 90831 Reversed number is 13809
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Practice BCA MCQs
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Check C Books