Logo 
Search:

Unix / Linux / Ubuntu Forum

Ask Question   UnAnswered
Home » Forum » Unix / Linux / Ubuntu       RSS Feeds

script

  Asked By: Zubair    Date: Apr 16    Category: Unix / Linux / Ubuntu    Views: 632
  

Write a shell script which gets digits in a single variable and prints the reverse of all digits for example, Input : 123 output 321

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Abhishek Singh     Answered On: May 14

#!/bin/bash# Shell program to read a number and reverse the number# for example 123 should output as 321# -----------------------------------------------# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/># This script is licensed under GNU GPL version 2.0 or above# -------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC)# Visit http://bash.cyberciti.biz/ for more information.# ------------------------------------------------------------------------- echo -n "Enter number : "read n # store single digitsd=0 # store number in reverse orderrev="" # store original numberon=$n # use while loop to caclulate the sum of all digitswhile [ $n -gt 0 ]do sd=$(( $n % 10 )) # get Remainder n=$(( $n / 10 )) # get next digit # store previoues number and current digit in rev rev=$( echo ${rev}${sd} ) done echo "$on in a reverse order $rev"

 
Didn't find what you were looking for? Find more on script Or get search suggestion and latest updates.




Tagged: