Reverse Number Program in C
Advertisements
Reverse Number Program in C
Reverse of number means reverse the position of all digits of any number. For example reverse of 839 is 938. Before start writing this code learn basic concept of While Loop in C
C Program to Reverse any Number Using While Loop
#include<stdio.h> #include<conio.h> void main() { int no,rev=0,r,a; clrscr(); printf("Enter any num: "); scanf("%d",&no); a=no; while(no>0) { r=no%10; rev=rev*10+r; no=no/10; } printf("Reverse of %d is: %d",a,rev); getch(); }
Output
Enter any num : 964 Reverse of 164 is 469
Explanation of program
Code
while(no>0) { r=no%10; rev=rev*10+r; no=no/10; }
In Above code we first check number is greater than zero, now find reminder of number and get last digits. After this step we place last digit at first position (at unit place) using "rev=rev*10+r". Again we divide number by 10 (no=no/10) and value are store in "no" variable. now we have a new number which have all digits except last digit. again check while loop and find remainder and get last digits. same process is repeated again and again until condition is true.
C Program to Reverse Number using for loop
#include<stdio.h> #include<conio.h> void main() { int no,rev=0,r,a; clrscr(); printf("Enter any num: "); scanf("%d",&no); a=no; for(;no>0;) { r=no%10; rev=rev*10+r; no=no/10; } printf("Reverse of %d is %d",a,rev); getch(); }
Output
Enter any num : 164 Reverse of 164 is 461
Google Advertisment