C Program to find the reverse of a number
#include <stdio.h> int main() { int num, reversed = 0, remainder; // Input from user printf("Enter an integer: "); scanf("%d", &num); while (num != 0) { remainder = num % 10; // Get the last digit reversed = reversed * 10 + remainder; // Build the reversed number num = num / 10; // Remove the last digit } printf("Reversed number: %d\n", reversed); return 0; } Output: Enter an integer: 1234 Reversed number: 4321
Explanation:
-
The program uses a
while
loop to extract the last digit of the number using the modulus operator%
. -
It then multiplies the reversed number by 10 and adds the last digit.
-
The original number is divided by 10 to remove the last digit.
-
This continues until the number becomes 0.