top

Search

C Tutorial

.

UpGrad

C Tutorial

C Program to Reverse a Number

Overview

How to find the reverse of a number in C using for loop? What is the reverse number program in C using if-else? Wondering what is the right program to find the reverse of a number?

The following article addresses all these questions and shares a few important concepts of the C program to reverse a number without using loop. Let’s get started!

Reverse a Number in C

To reverse a number in C is to simply change the order of its digit. For example, if we have a number 3489, then the reversed number would be 9843. To reverse a number in C, you need to follow the steps you mentioned:

  • Initialise a variable to store the reversed number.

  • Use the modulo operator to extract the last digit of the original number.

  • Multiply the reversed number by 10, and then add the extracted digit.

  • Divide the original number by 10 to remove the last digit. 

  • Continue using the last three steps until all the digits of the original number have been thoroughly processed. 

  • The desired result will be generated, which is the final value of the reversed number.

Flowchart of C Program to Reverse a Number

The flowchart for a C program to reverse a number is provided below:

Flowchart to Reverse a Number

Algorithm to Reverse a Number in C

Here is a small algorithm to reverse a number in C:

  • Start

  • Read the input number from the user

  • Initialise a variable named ‘reversedNumber’ to 0

  • While the input number is not equal to 0, repeat steps 5-7

  • Extract the last digit of the input number using the modulo operator and store it in a variable named 'lastDigit'

  • Multiply the 'reversedNumber' by 10

  • Add the 'lastDigit' to the 'reversedNumber'

  • Divide the input number by 10 to remove the last digit

  • End the loop

  • Print the value of the 'reversedNumber'

  • Stop

This algorithm ensures that each digit of the input number is extracted, and the reversed number is built by multiplying the existing reversed number by 10 and adding the current digit. The loop continues until all the digits of the input number have been processed, resulting in the reversed number.

How to Implement in C Programming?

Here’s the C program to reverse a string or how to implement the algorithm for reversing a number.

#include <stdio.h>
int main() {
    int number, reversedNumber = 0, remainder;
    printf("Enter a number: ");
    scanf("%d", &number);
    while (number != 0) {
        remainder = number % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        number /= 10;
    }
    printf("Reversed number: %d\n", reversedNumber);
    return 0;
}

Here, we have generated the ‘number’ variable to store the original number and ‘reversedNumber’ is initialised as 0 to store the reversed number. Following this, using the modulo operator and the ‘remainder’ variable, we have extracted the last digit of the original number. 

Inside the while loop, by calculating "number % 10”, we have extracted the last digit of the ‘number’ and stored the same in ‘remainder’. The ‘reversedNumber’ is then updated by multiplying it by ten and adding the ‘remainder’. Finally, to remove the last digit, we have divided the ‘number’ by 10. 

This process continues till the ‘number’ becomes 0, denoting that all the numbers have been successfully processed. The end result is then generated, which is ‘reversedNumber’, containing the reversed form of the original number. 

Recursive Implementation of Reversing a Number Using 

A recursive implementation of reversing a number using C basically means implementing the reversal process using recursion. In such a case, the function calls itself to solve a compact version of the main problem until the base case is reached, following which the recursion gets terminated. 

Let’s explore a small example of how you can use recursive implementation to reverse a number in C.

#include <stdio.h>
int reverseNumber(int number, int reversedNumber) {
    if (number == 0) {
        return reversedNumber;
    }
    
    int lastDigit = number % 10;
    reversedNumber = reversedNumber * 10 + lastDigit;
    
    return reverseNumber(number / 10, reversedNumber);
}
int main() {
    int number, reversedNumber;
    
    printf("Enter a number: ");
    scanf("%d", &number);
    
    reversedNumber = reverseNumber(number, 0);
    
    printf("Reversed number: %d\n", reversedNumber);
    
    return 0;
}

Issue with Negative Number Inputs

One of the main problems faced while trying to reverse a number in C using recursion is that it does not handle negative numbers input correctly. Although the recursion process will continue until the number becomes 0, the reversed number will not be calculated correctly. 

To successfully tackle this challenge, you must modify the code to take the absolute value of the number before the modulo operation is performed. This will ensure that the remainder is always positive and the reversal calculator will work as required. 

Analysis of Algorithm

Here is a small example of modifying the algorithm to address the issue with a negative number in C.

#include <stdio.h>
#include <stdlib.h>
int reverseNumber(int number, int reversedNumber) {
    if (number == 0) {
        return reversedNumber;
    }
    
    int lastDigit = abs(number) % 10;
    reversedNumber = reversedNumber * 10 + lastDigit;
    
    return reverseNumber(number / 10, reversedNumber);
}
int main() {
    int number, reversedNumber;
    
    printf("Enter a number: ");
    scanf("%d", &number);
    
    reversedNumber = reverseNumber(number, 0);
    
    printf("Reversed number: %d\n", reversedNumber);
    
    return 0;
}

As quite visible, here we have included the header file, ‘stdlib.h’, to access the ‘abs()’ function that will return the absolute value of a number. This absolute value of the ‘number’ plays a crucial role as it helps to ensure that the modulo operation, ‘abs(number) % 10’, works accurately, irrespective of whether the number is positive or negative. 

Therefore now it can handle the negative number inputs correctly and generate the reversed number accordingly.

Final Words

There are numerous ways by which you can reverse a number in C. Be it the reverse of a number in C using a loop or a C program to reverse a number using function, the logic behind the calculations stays the same always. It is just one of the many basic concepts of the C programming language. 

To gain in-depth knowledge about the same, you can check out the MS in Computer Science course offered upGrad, under the guidance of Liverpool John Moores University. The program is equipped to strengthen your programming skills with an immersive learning experience and hands-on experience with practical applications. Post completion of this program, you will be rewarded with exciting career opportunities to become a part of the industry leaders’ clan!

FAQs

1: What do we mean by the reverse array in C?

Unfortunately, there is no built-in function to reverse an array in C. We can generate a code for the same using multiple methods and algorithms. For example, by enabling recursion or using an auxiliary array, we can reverse the array in C.

2: What do we mean by the loop in C?

Ans: In C programming language, a loop helps to execute the block of code multiple times, according to the specified condition. This helps to traverse the elements of an array and also saves the code for future use. 

3: Can you explain the array in C?

Array is the culmination of elements of similar types into a large group. Such include double data type and user-defined data type structures. 

Leave a Reply

Your email address will not be published. Required fields are marked *