Results 1 to 6 of 6

Thread: Shell program to reverse a number

  1. #1
    Join Date
    Dec 2009
    Posts
    67

    Shell program to reverse a number

    Hello Friends,

    I am learning the Shell script programming. It seems quit difficult to code the shell programs than codding c programs. I have to write the one shell program to reverse the given number. Suppose if you provides '1234' as a input to the shell program it should return '4321' as a output result. I have tried lots with no luck. Do you have sound knowledge about shell programming. Please help me to code shell program for reverse number.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Shell program to reverse a number

    Below I have given one shell program to print given number in reverse order, see below:
    echo "Enter one Number"
    read nl
    nl2=0
    while [ $nl - ne 0 ]

    do
    nl2=`expr $nl % 10 + $nl2 \* 10`
    nl=`expr $nl / 10`
    done

    echo $nl2

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Shell program to reverse a number

    Shell program to reverse a number:
    echo -p "provide number : "
    read p

    sd1=0

    rev1=""

    on=$p

    while [ $p -gt 0 ]

    do

    sd1=$(( $p % 10 ))
    n1=$(( $p / 10 ))
    rev1=$( echo ${rev1}${sd} )

    done

    echo "$reverse order= $rev1"

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Shell program to reverse a number

    Refer to following shell script program. In this program I have used on expression to make number reverse. First I divided the given number 'x' by ten then I multiplied by 'x2' number. After that I reassigned the value of 'x/10' to the number 'x'. For this I have used 'while' loop.
    echo "Type Number"
    read x // accept value for x
    x2=0

    while [ $num -ne 0 ]
    do

    x2 =`expr $x % 10 + $x2\* 10`// use while loop to calcualate reverse number
    x =`expr $x / 10`

    done
    echo $x2

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: Shell program to reverse a number

    Hi friend,

    Please study carefully C program which prints the reverse number. Don't copy the entire program to code shell program for reverse number. I suggest you to just refer the programming logic that I used in this program. You need to implement the same logic in your shell program.
    void main()
    {
    int w, s, d, f, g;

    printf("Enter Number to Find it's Reverse number\n");
    scanf("%d",&w);

    while(w!=0)
    {
    s=a%10;
    d=a/10;

    printf("%d",s);

    w=d;
    }

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Shell program to reverse a number

    Shell program code to reverse a number:
    if [ $# -ne 1 ]
    then

    fi

    m=$1
    rv=0
    smd=0

    while [ $n -gt 0 ]
    do
    smd=`expr $nm % 10`
    rv=`expr $rv\* 10 + $sd`
    nm=`expr $nm/ 10`
    done
    echo "Reverse number is $rv"

Similar Threads

  1. How to run the Turbo C++ v3.0 program on DOS Shell
    By Faiyaz9 in forum Software Development
    Replies: 5
    Last Post: 31-08-2011, 10:19 PM
  2. Java program to reverse the order of LinkedList elements?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:44 PM
  3. Shell program for the prime number
    By Rup_me in forum Software Development
    Replies: 5
    Last Post: 14-12-2009, 10:24 AM
  4. What is the shell program to calculate addition of two numbers?
    By Shophia_D in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 02:34 PM
  5. program to reverse a string in java.
    By Deepest BLUE in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 11:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,300,640.27716 seconds with 17 queries