C Program to reverse a given number using Recursion

Here is the C program to reverse a number:

				
					#include <stdio.h>

int main() {
  int n, reverse = 0, remainder;

  printf("Enter a number: ");
  scanf("%d", &n);

  while (n != 0) {
    remainder = n % 10;
    reverse = reverse * 10 + remainder;
    n /= 10;
  }

  printf("Reversed number: %d\n", reverse);

  return 0;
}

				
			
Output
C Program to Reverse a Number

This program works by first getting the number from the user. Then, it uses a loop to reverse the digits of the number. Finally, it prints the reversed number.

Here is a breakdown of how the program works:

  • The printf() function is used to print the prompt “Enter a number: “.
  • The scanf() function is used to read the number from the user.
  • The while loop is used to reverse the digits of the number. In each iteration of the loop, the remainder of n when divided by 10 is calculated and stored in the variable remainder. The value of reverse is then multiplied by 10 and the value of remainder is added to it. Finally, the value of n is divided by 10.
  • The printf() function is used to print the reversed number.
  • The return 0 statement is used to indicate that the program has completed successfully.