This Is one of Very Basic Programming Problem for beginner . But If you never Did it , Try it once.
Reverse Given Number :-
Example >> Let Number is 12345 then after reversing Number Should be 54321
There is two way to do it
Reverse Given Number :-
Example >> Let Number is 12345 then after reversing Number Should be 54321
There is two way to do it
Way 1:-
Pop Last Digit then push it to another variable and repeat procedure until number == 0
- temp = num%10
- num = num/10
- numr = numr*10 + temp
- repeat above step until num==0
C++ Program to Reverse integer :
- // Reverse a Number
- //Ex:- 12345 -> 54321
- #include<iostream>
- using namespace std;
- main(){
- long int num,numr=0;
- int temp;
- cout<<"Enter Number : ";
- cin>>num;
- // Reverse Number :- just pop last digit and push it to numr,
- //repeate above step untill num=0
- while(num>0){
- temp=num%10;
- num =num/10;
- numr=(numr*10)+temp;
- }
- cout<<"Revrsed number :"<<numr;
- return 0;
- }
Way 2 :-
Convert number In string then reverse it than change it to integer . In some programming language it is very easy .
Python :- Reverse Integer :-
- # Reverse an interger
- if __name__=='__main__':
- num=int(raw_input('Enter Number: '))
- numr=str(num)[::-1] #reverse String
- numr=int(numr)
- print 'reverse integer :',numr
OutPut :-
Reverse Integer |
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT