Data structures & algorithms (Beginner to Intermediate)

Free guided path13 chapters99+ problems

Earn badges and level up

Introduction

Reversing a number implies that the value of the unit of a given number will become the nth digit value of the number if it is n digits long, and the tens digit number will become (n-1)^{th} place value number and so on. It implies printing the digits of a specified number from the back to the front. For example, if the given number is 987, the reverse of that number is 789.

Now, before moving on, let us discuss the relevance of reverse a number. Sometimes we are stuck upon a code where we have to find the palindrome of a number. If you are unfamiliar with the concept of palindrome, then please note that a palindrome is a number that is the same even after its reversal.

For example, number 4554 is a palindrome of 4554. So, in this case, it becomes mandatory for us to fetch all the possible values which contain palindrome numbers and hence here comes the need to find the reversal of the number.

#include <stdio.h>
int main() {
int num, reversedNum = 0, remainder;
// Input the number
printf("Enter an integer: ");
scanf("%d", &num);
// Reverse the number
while (num != 0) {
remainder = num % 10; // Extract the last digit
reversedNum = reversedNum * 10 + remainder; // Build the reversed number
num = num / 10; // Remove the last digit
}
// Output the reversed number
printf("Reversed number: %d\n", reversedNum);
return 0;
}

This program takes an integer as input, iteratively extracts the last digit (remainder), builds the reversed number by multiplying the current reversed number by 10 and adding the remainder, and finally removes the last digit from the original number. This process continues until the original number becomes zero.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

Now, Let us learn to write a C program to reverse a number.

1. C Program to Reverse a Number Using While Loop

Reversing a number in C is really simple, the logic is elementary, which is that we take the rightmost digit of the number, add it to the reverse result number, multiply it by 10, and add the remainder, if any. Followed by the removal of the rightmost digit; this process goes on until the number becomes zero.

Letâ€™s understand this with the help of an example:

#include <stdio.h>
int main() {
int number, reverse = 0, temp; //temp is the remainder, and reverse is the result
printf("Enter Number to Reverse:");
scanf("%d", &number);
while (number != 0){ //processed until the number becomes zero
temp = number % 10; //getting the rightmost digit
reverse = reverse * 10 + temp;
number = number/10;
}
printf("Number after Reversing = %d", reverse);
return 0;
}

Input

Enter Number to Reverse:12345

Output

Number after Reversing = 54321

2. C Program to Reverse a Number Using For Loop

Reversing a number using a For Loop has similar logic to that of a While Loop; that is, we pop the rightmost digit of the number, multiply it by 10, and add it to the resultant reversed number along with the remainder, if any. Following is the code example of Reversing a Number using For Loop:

#include <stdio.h>
int main() {
int number, reverse = 0, temp;
printf("Enter the number to be reversed: ");
scanf("%d", &number);
for (; number != 0; number /= 10) { //As the number is already initialised, so not declared in for loop
temp = number % 10; //Getting the remainder, along with right most digit
reverse = reverse * 10 + temp;
}
printf("Result after Reversing = %d", reverse);
return 0;
}

A recursive function solves a problem by calling a copy of itself to work on a smaller problem. Each time a function calls itself, a simpler version of the original problem is shown. All these smaller problems converge on a base case.

The idea is to use recursion to perform the task for a single digit of the number, and let Recursion handle all the numbers on itâ€™s own.

Algorithm

Take the number N as input from the user.

Create a function for reversing a number that accepts the input N as its parameter.

Initialise the reverse number to 0.

Check if N is greater than 0, and update the already existing reverse number by multiplying it by 10.

And then add the reverse number with the last digit of the number N by taking modulo 10.

Keep on calling the recursive function until N becomes less than 0.

Finally, return the reverse number and print it as output.

Pseudo Code

Input=N
Reverse (int N) // Recursive function accepting input as its parameter
rev_num=0
// While input N is greater than 0:
rev_num=rev_num*10
rev_num=rev_num + N modulo 10
Reverse (N/10)
print(rev_num)

Example

Here, in this example, we are going to reverse a number using recursion. In this example, we will create a recursive function â€˜reverseâ€™ which will take an integer and will reverse the given number.

#include <stdio.h>;
int reverse(int num)
{
static int revNum = 0;
if (num>0)
{revNum = revNum*10;
revNum += (num % 10);
reverse(num / 10);
return revNum;
}
else
return 0;
}
int main()
{
int num=1289;
printf(reverse(num));
return 0;
}

Output

9821

In the above code, we have taken the function reverse which takes an integer number as input, also we have already given the number ' 1289' to the function. We checked if the number is greater than zero, then broke down the given number and stored the output in the variable "revNum" after this we returned the variable "revNum".The output of the above code is "9821".

4. C Program to Reverse a Number Using Function

To reverse a number using a function, we must first create a function, for example reversing_a_number(), that takes the number as a parameter. Further, the same approach similar to For Loop or While Loop can be implemented in the function.

Further, in the main () function, the number is taken as an input, a function is called on that number, and the return result of that function is stored in another variable, which will be the final output of the code.

Example

#include <stdio.h>
int main() {
int number, reverse;
printf("Enter a Number to reverse: ");
scanf("%d", &number);
reverse = reversing_a_number(number); //Function is being called, with number as the parameter
printf("Number after Reversing = %d", reverse);
return 0;
}
int reversing_a_number(int number) { //Function is implemented for reversing a number
int reverse = 0, temp; //reverse is the result, temp is the remainder
while (number != 0) {
temp = number % 10; //Same logic as of While/For Loop
reverse = reverse * 10 + temp;
number /= 10;
}
return reverse;
}

First, initialise two integer variables like (number, temp). Now perform the reverse operation with both the variables. Finally, show the result after reversing as output.

Which algorithm is used to reverse a given number?

The algorithm to reverse a number is to get the digit at the extreme right, multiply it by 10, and pop it out of the original number. This process needs to be followed until every digit of that number is traversed.

What is the logic of reverse number in C?

The logic is pretty simple to reverse a number, that is to get the extreme right digit of that number, multiply it with 10, add a remainder if any, and place it in the new reversed number. This process needs to be followed until the number is reduced to zero.

How to reverse a number in C without using reverse function?

Reversing a function in C without using any reverse function is absolutely possible; this can be done using a While or For Loop. The primary condition in the loop should be that it should run until the number is reduced to zero.

Conclusion

In this article, we studied everything that is needed to know about a C program to reverse a number. This is the most optimal and widely used approach for reversing the digits of a number.

The process is almost the same in every programming language, and therefore, once you know the logic of the program, it will be straightforward for you to implement it.