Explanation of the program
This program takes a number as input, and prints it in the reverse order. The number is read into the variable n. Inside the while loop, the remainder after the division of n by 10 is calculated by using the % (modulo) operator and it is stored in the variable a, this will give the last digit of any number. Next n is divided by 10 to avoid the last digit, and the computation is continued with the remaining digits. Each time after obtaining the last digit, it is printed. The loop terminates when the value of n is less then or equal to 0.
Program
/*reverse a number c program*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); int n,a; printf("Enter the number"); scanf("%d",&n); printf("Reverse is"); while(n>0) { a=n%10; printf(""); printf("%d",a); n=n/10; } getch(); }
Output
C program to find the reverse of a number |