reverse an integer


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reverse an integer
# 1  
Old 05-20-2008
reverse an integer

i have created a script that will reverse any given ineter.

#!/bin/ksh

echo "Enter the number"
read n

if [ $n -gt 0 ]
then
a=`expr $n / 10`
b=`expr $n % 10`
c=`expr $b \* 10 + $a`
fi
echo $c
---------------------------------------------------------------------

the problem with this script is that it only gives o/p for 2 digit number.

Can anyone help me in modifying this script that it will give o/p for any number of digit
.
# 2  
Old 05-20-2008
#!/bin/ksh

echo "Enter the number"
read n
c=0
while [ $n -gt 0 ]
do
a=`expr $n % 10 `
n=`expr $n / 10 `
c=`expr $c \* 10 + $a`
done
echo $c



Thanks
Penchal
# 3  
Old 05-20-2008
Thanks a lot for ur help dear. If possible can u tell me why there is a problem with my script...........
# 4  
Old 05-20-2008
Code:
perl -nle 'print scalar reverse'

The "scalar" is necessary for making reverse operate on a string; by default, it reverses a list.
# 5  
Old 05-20-2008
there must be loop( while) instead of condition (if)

In the last two lines there is logic problem

b=`expr $n % 10`
c=`expr $b \* 10 + $a`


Thanks
Penchal
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reverse even lines

I'm trying to reverse every even line in my file using the awk command below but it prints only the odd lines but nothing else: $ awk '(NR % 2) {print}; !(NR % 2) {print | "rev";}' myfile Any idea what I might have done wrong? Thank you. (10 Replies)
Discussion started by: ivpz
10 Replies

2. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

3. Programming

String reverse

Hi all, I jus wanna print string b after reversing it. but the out put is blank. My code snippet is below. :wall: int main() { char * a, * b; b = new char; a = new char; int len, le; le = 0; cout<< " enter your string \n"; cin>> a; len = strlen(a); for(int i =... (8 Replies)
Discussion started by: vineetjoshi
8 Replies

4. Shell Programming and Scripting

Reverse multiword

Just check out the script... 1 #!/bin/bash 2 3 echo -n "Enter a string :: " 4 read str 5 echo -n "Reverse is :: " 6 l=`expr length "$str"` 7 while 8 do 9 m=`echo $str | cut -c "$l"` 10 echo -n $m 11 ... (3 Replies)
Discussion started by: lipun4u
3 Replies

5. Shell Programming and Scripting

How to reverse output?

hi, I have to reverse the command output like below: output: online offline disable maintening killed How to reverse this output like: killed maintening disable offline online It should be ksh script. (4 Replies)
Discussion started by: a2156z
4 Replies

6. Shell Programming and Scripting

how to reverse file

i am using AIX -ksh how can i reverse any file ,i have already try tac cmd it is not in AIX: please help me out. (3 Replies)
Discussion started by: RahulJoshi
3 Replies

7. Shell Programming and Scripting

Reverse lookup

hey guys, can anybody help me out here on the following: grep '^\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}$' ravi.txt mary.txt lisa.txt https://www.unix.com/images/misc/progress.gif i.e what i did was found ip addreses from different files and then i want... (1 Reply)
Discussion started by: ravis83
1 Replies

8. Shell Programming and Scripting

reverse sort

Hello, How do i sort a csv file. i should be sorting column1(varchar),column2*(varchar) in ascending and column4 in descending order(numeric datatype). I tried few combinations of sort, but doesn't seem to be getting the right result. sort -t "," -k 1 -k 2 -k 4nr file any help is... (3 Replies)
Discussion started by: markjason
3 Replies

9. Shell Programming and Scripting

Reverse FTP

Hi Everybody, I want to write a script in unix which will automatically FTP a .txt file from my client machine D: drive(Windows) That is I want to FTP a file from my PC to UNIX box but this should be done from UNIX box by a shell script. (i.e. I will invoke the script in UNIX and FTP will be... (4 Replies)
Discussion started by: ganesh123
4 Replies

10. Shell Programming and Scripting

Reverse *

when I do $ ls z* List of all files begining with 'z'. But what if I want to do a reverse lookup. Just for interest sake ;) $ ls ztr should be same as $ ls ztr* $ ls zt* $ ls z* (2 Replies)
Discussion started by: azmathshaikh
2 Replies
Login or Register to Ask a Question