## Shell Script To Simulate a Simple Calculator

in Categories Academic, Shell Math last updated October 20, 2008

Shell Program to simulate a simple calculator operations such as addition and multiplication.

## Shell Script To Find Largest Among 3 Integers (Numbers) Given As Arguments

in Categories Academic, Decision Making, Shell Math last updated October 20, 2008

Shell code to find largest integer among the 3 integers given as arguments.

## Shell Script to swap values in two variables x and y

in Categories Shell Math last updated April 9, 2008
```#!/bin/bash
# Shell Script to swap values in two variables x and y
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# -------------------------------------------------------------------------
echo "Enter value for x : "
echo "Enter value for y : "
echo "Before swap, x = \$x and y = \$y"
z=\$x
x=\$y
y=\$z
echo "After swap, x = \$x and y = \$y"```

## Shell script to find whether an input number is palindrome or not

in Categories Academic, Shell Math last updated April 12, 2008

A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted). Composing literature in palindromes is an example of constrained writing. The word “palindrome” was coined from Greek roots palin (“back”) and dromos (“way, direction”) by English writer Ben Jonson in the 1600s.

A palindromic number is a number where the digits, with decimal representation usually assumed, are the same read backwards, for example, 58285. They are studied in recreational mathematics where palindromic numbers with special properties are sought. A palindromic prime is a palindromic number that is a prime number.

```#!/bin/bash
# Shell script to find whether an input number is palindrome or not
# -----------------------------------------------
# Copyright (c) 2006 nixCraft <http://cyberciti.biz/fb/>
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# -------------------------------------------------------------------------

echo -n "Enter number : "

# store single digit
sd=0

# store number in reverse order
rev=""

# store original number
on=\$n

while [ \$n -gt 0 ]
do
sd=\$(( \$n % 10 )) # get Remainder
n=\$(( \$n / 10 ))  # get next digit
# store previous number and current digit in reverse
rev=\$( echo \${rev}\${sd} )
done

if [ \$on -eq \$rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi```

## Shell program to read a number and find the sum of digits

in Categories Academic, Shell Math last updated April 8, 2008
```#!/bin/bash
# Shell program to read a number and find the sum of digits
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# -------------------------------------------------------------------------

echo -n "Enter number : "

# store single digit
sd=0

# store number of digit
sum=0

# use while loop to caclulate the sum of all digits
while [ \$n -gt 0 ]
do
sd=\$(( \$n % 10 )) # get Remainder
n=\$(( \$n / 10 ))  # get next digit
sum=\$(( \$sum + \$sd )) # calculate sum of digit
done
echo  "Sum of all digit  is \$sum"```

## Shell program to read a number and reverse the number

in Categories Academic, Shell Math last updated April 8, 2008
```#!/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 part of nixCraft shell script collection (NSSC)
# -------------------------------------------------------------------------

echo -n "Enter number : "

# store single digit
sd=0

# store number in reverse order
rev=""

# store original number
on=\$n

# use while loop to caclulate the sum of all digits
while [ \$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"```

## Shell program to calculate the number of digits in a number read from the user

in Categories Academic, Shell Math last updated April 8, 2008
```#!/bin/bash
# Shell program to calculate the number of digits in a
# number read from the user
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# -------------------------------------------------------------------------

echo -n "Enter number : "

sd=0

# store number of digit
nd=0
on=\$n # store \$n so that we can use it later

# use while loop to caclulate the number of digit
while [ \$n -gt 0 ]
do
sd=\$(( \$n % 10 )) # get Remainder
n=\$(( \$n / 10 ))
nd=\$(( \$nd + 1)) # calculate all digit in a number till n is not zero
done
echo  "Numnber of digit in a \$on is \$nd"```

## Shell script to read 5 digit number and calculate the sum of digit

in Categories Shell Math last updated April 4, 2008
```#!/bin/bash
# Shell script to read 5 digit number and calculate the sum of digit
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# -------------------------------------------------------------------------

echo -n "Enter numnber : "

# find out length of string using wc -c command
len=\$(echo \$n | wc -c)

# remove \n i.e. new line character
len=\$(( \$len - 1 ))

# use loop to go throug all digit one by one and calculate sum of digit on fly
for (( i=1; i <= \$len; i++ ))
do
sum=\$(( \$sum + \$(echo \$n | cut -c \$i) ))
done
echo "Sum of \$n is \$sum"```

## Shell Script Calulate Area and Circumference of Circle

in Categories Shell Math last updated April 4, 2008

A circle is one of the simple shapes of Euclidean geometry. It is the locus of all points in a plane at a constant distance, called the radius, from a fixed point, called the center. Through any three points not on the same line, there passes one and only one circle.

A diameter is a chord passing through the center. The length of a diameter is twice the radius. A diameter is the largest chord in a circle.

Circles are simple closed curves which divide the plane into an interior and an exterior. The circumference of a circle is the perimeter of the circle, and the interior of the circle is called a disk. ## Sample Shell Script To Calculate Area and Circumference of Circle

```#!/bin/bash
# Shell script to calulate area and Circumference of circle.
# It take radius of a circle as input.
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# ----------------------------------------------------------------------
# Lesson on Circumference of a Circle :
#  Visit http://www.mathgoodies.com/lessons/vol2/circumference.html
echo -n "Enter the radius of a circle : "