Reverse number
Reverse of number?
I am going to show how to reverse the given number using c program to write any program we first need to know the logic.
If we have to reverse the 123 so how we do it?
divide the number by 10 i.e. 123/10
let rev. be the number after reverse so this time value of rev. is zero
therefore, 10*rev.+reminder
so, 10*0+3=3
repeat the above step until given number is not equal to zero.
The result is 321 now the time to modify this logic into c program
Program :-
#include<stdio.h> #include<conio.h> main() { int number,r,reverse=0,n; printf("enter the number: "); scanf("%d",&number); r=number%10; while(r!=0) { reverse=reverse*10+r; number=number/10; r=number%10; } printf("reverse is:%d ",reverse); getch(); }
Output :-
enter the number: 465
reverse is: 564
let us try for four digit number
enter the number: 1234
reverse is: 4321
here is the program and output
No comments