Monday, August 5, 2013

HOW TO REVERSE A NUMBER


Here is a simple shell script to reverse the digits of the number entered by the user.

echo "Enter the number to be reversed where the first digit should have range from 1-9"   #prompt the message to enter the 
number such that first digit is not zero
read number                                                       #enter the number
var=${#number}                                               #find the number of the digits in the  number                                                           
for((i=1;i<=$var;i++))                                       # initiate the loop
do
var1=$((number%10))                                        #find the last digit of the number
echo -n "$var1"                                                 #display the last digit such that output should not trail to the new line.
number=$((number/10))                                  #remove the last digit     
done                                                                    #terminate the loop
echo                                                                  #change to new line


The above script is simple technique to reverse any number entered by the user.
Note : The script can be executed by giving execute permission as below:
                     # chmod +x Script_Name
        # ./Script_Name
                         OR 
Executing the script directly as follows:
                      # bash Script_Name
                             OR
                     #  sh Script_Name

0 comments:

Post a Comment