Reverse integer in c

Reversing an Integer in C

To reverse an integer in C, you can use the following steps:

  1. Input the Integer: First, input the integer that you want to reverse.

  2. Initialize Variables: Initialize variables to store the original and reversed numbers, and a variable to store the remainder when the original number is divided by 10.

  3. Reverse the Integer: Use a while loop to reverse the integer. Inside the loop, obtain the last digit of the original number using the modulo operator, then build the reversed number by multiplying it by 10 and adding the last digit of the original number.

  4. Output the Reversed Integer: Finally, output the reversed integer.

Here's a sample code snippet that demonstrates this process:

#include <stdio.h>

int main() {
    int originalNumber, reversedNumber = 0, remainder;

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

    while (originalNumber != 0) {
        remainder = originalNumber % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        originalNumber /= 10;
    }

    printf("Reversed number = %d\n", reversedNumber);

    return 0;
}

This code takes an integer as input, reverses it, and then outputs the reversed number.

[[SOURCE 1]]