C Program to Reverse a Number

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”.

Methods for Reversing a Number in C

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.

Method 1: Reverse a Number in C using While Loop

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.

advertisement
advertisement

For example, 323441 becomes 144323

Program/Source Code

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.

  1. /* C program to reverse a number */
  2.  
  3. #include <stdio.h>
  4.  
  5. int main(void)
  6. {
  7.     long num, reverse = 0, temp, remainder;
  8.     printf("Enter the number:\n");
  9.     scanf("%ld", &num);
  10.     temp = num;
  11.     while (num > 0)
  12.     {
  13.         remainder = num % 10;
  14.         reverse = reverse * 10 + remainder;
  15.         num /= 10;
  16.     }
  17.     printf("Given number = %ld\n", temp);
  18.     printf("Reverse of a Number is %ld\n", reverse);
  19. }
Program Explanation

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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).

Runtime Test Cases

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.

advertisement
Enter the number
42394
Given number = 42394
Reverse of a Number is 49324

Method 2: Reverse a Number in C using For Loop

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.

advertisement
Program/Source Code

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;
}
Program Explanation

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).

Runtime Test Cases

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

Method 3: Reverse a Number in C using Function

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.

Program/Source Code

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.

  1. /* C program to reverse a number using function*/
  2.  
  3. #include <stdio.h>
  4.  
  5. int reverse(int n)
  6. {
  7.     int rev = 0;
  8.     while (n > 0)
  9.     {
  10.         rev = rev * 10 + n % 10;
  11.         n /= 10;
  12.     }
  13.     return rev;
  14. }
  15.  
  16. int main(void)
  17. {
  18.     int n;
  19.     printf("Enter a number: ");
  20.     scanf("%d", &n);
  21.     printf("Reversed number is %d", reverse(n));
  22.     return 0;
  23. }
Program Explanation

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).

Runtime Test Cases

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%

Method 4: Reverse a Number in C using Recursion

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.

Program/Source Code

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.

  1. /* C program to reverse a number using recursion */
  2.  
  3. #include <stdio.h>
  4.  
  5. int reverse(int n, int rev)
  6. {
  7.     if (n == 0)
  8.         return rev;
  9.     rev = rev * 10 + n % 10;
  10.     return reverse(n / 10, rev);
  11. }
  12.  
  13. int main(void)
  14. {
  15.     int n;
  16.     printf("Enter a number: ");
  17.     scanf("%d", &n);
  18.     printf("Reversed number is %d", reverse(n, 0));
  19.     return 0;
  20. }
Program Explanation

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.

Runtime Test Cases

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”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.