C - Program - Reverse Number

To reverse the digits of a number in C, you can use arithmetic operations and a loop. Here's an example program that reverses the digits of a given number:

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

In this program, the reverseDigits() function takes an integer number as input and reverses its digits. It initializes a variable reversedNumber to 0, which will hold the reversed number. The function then uses a loop to extract the last digit of the number using the modulo operator (number % 10) and appends it to the reversedNumber by multiplying it by 10 and adding the digit. It then divides the number by 10 to remove the last digit. The loop continues until the number becomes 0.

The reversed number is then returned from the function.

In the main() function, we input a number from the user. We call the reverseDigits() function, passing the input number, and store the returned reversed number in a variable. Finally, we print the reversed number.