Sure! Here’s a C program that reverses a given number:
#include <stdio.h> int reverseNumber(int num) { int reversedNum = 0; while (num != 0) { int remainder = num % 10; reversedNum = reversedNum * 10 + remainder; num /= 10; } return reversedNum; } int main() { int num, reversedNum; printf("Enter a number: "); scanf("%d", &num); reversedNum = reverseNumber(num); printf("Reversed number: %d\n", reversedNum); return 0; }
In this program, the reverseNumber
function takes an integer num
as input and returns the reversed number. It uses a while loop to reverse the number by continuously extracting the last digit (remainder
) using the modulo operator %
and then adding it to the reversedNum
variable after shifting it one place to the left. Finally, the function divides num
by 10 to remove the last digit. This process continues until num
becomes 0.
In the main
function, the user is prompted to enter a number. The scanf
function is used to read the input. The reverseNumber
function is called with the entered number, and the reversed number is stored in the reversedNum
variable. Finally, the reversed number is printed to the console using printf
.