Reverse a Number in C

Reversing a number is a common problem in programming that can help in understanding loops, conditions, and data manipulation. In this article, we will explore three different ways to write a program to Reverse a Number in C: using a loop, using recursion, and using string manipulation.

Learn how to “Reverse a Number in C”

Method 1) Reverse a Number in C using a Loop

This is the most straightforward approach, where we use a loop to reverse the digits of the number.

Code Example

#include <stdio.h>

int main() {
    int num, reversed = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &num);

    while (num != 0) {
        remainder = num % 10;
        reversed = reversed * 10 + remainder;
        num /= 10;
    }

    printf("Reversed Number: %d\n", reversed);
    return 0;
}

Explanation of the Code

  1. Input: The user inputs an integer.
  2. Loop: The while loop runs as long as the number is not zero.
    • Remainder: Extracts the last digit using num % 10.
    • Reversed: Adds the remainder to the reversed number, multiplying the current reversed number by 10 to shift digits.
    • Update Number: Removes the last digit from the number using num /= 10.
  3. Output: The reversed number is printed.

This method is efficient and easy to understand, making it suitable for beginners.

Method 2) Reverse a Number in C using Recursion

Recursion provides a more elegant solution by breaking down the problem into smaller instances of the same problem.

Code Example

#include <stdio.h>

int reverseNumber(int num, int reversed) {
    if (num == 0) {
        return reversed;
    }
    reversed = reversed * 10 + num % 10;
    return reverseNumber(num / 10, reversed);
}

int main() {
    int num;

    printf("Enter an integer: ");
    scanf("%d", &num);

    printf("Reversed Number: %d\n", reverseNumber(num, 0));
    return 0;
}

Explanation of the Code

  1. Function Definition: reverseNumber is a recursive function that takes the number and the current reversed number as arguments.
  2. Base Case: If the number is zero, return the reversed number.
  3. Recursive Case:
    • Update Reversed: Add the last digit to the reversed number.
    • Recursive Call: Call the function again with the number divided by 10.
  4. Main Function: Calls the recursive function with the initial number and reversed number set to 0.

This method is more compact and showcases the power of recursion in solving problems by reducing them to simpler subproblems.

Reverse a Number in C using String Manipulation

Another approach is to convert the number to a string, reverse it, and then convert it back to an integer.

Code Example

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char numStr[50], revStr[50];
    int len, i, num;

    printf("Enter an integer: ");
    scanf("%s", numStr);

    len = strlen(numStr);
    for (i = 0; i < len; i++) {
        revStr[i] = numStr[len - i - 1];
    }
    revStr[i] = '\0';

    num = atoi(revStr);
    printf("Reversed Number: %d\n", num);
    return 0;
}

Explanation of the Code

  1. Input: The user inputs a number as a string.
  2. String Length: Determine the length of the string.
  3. Reverse Loop: Reverse the string using a loop.
  4. Convert to Integer: Convert the reversed string back to an integer using atoi.
  5. Output: Print the reversed number.

This method is useful when working with larger numbers or when the number needs to be treated as a string for other operations.

Categories C

Leave a Comment