#include <stdio.h>
int main(){
//18. Write a program to reverse a number.
int n, reverse=0 ,remainder;
printf("\tReverse A Number");
printf("\n========================================");
printf("\nEnter The Number : ");
scanf("%d",&n);
while(n != 0){
remainder = n%10;
reverse = (reverse*10) + remainder;
n /= 10;
}
printf("\n__________________________________________");
printf("\n\nReversed Number Is : %d",reverse);
printf("\n\n__________________________________________\n\n");
return 0;
}
reverse
, remainder
, and n
are declared and initialized to 0. In each iteration of the loop, the program calculates the remainder of the number when divided by 10, and then updates the reverse
variable by multiplying it by 10 and adding the remainder. The program then divides the original number n
by 10 and continues the loop until n
becomes 0. Finally, the program prints the reversed number to the console along with a message. The program ends by returning 0 to indicate that it has executed successfully.