0% found this document useful (0 votes)
2 views

Unix Shell Program

The document contains various shell programs that perform different tasks such as checking if a number is even or odd, finding palindromes, calculating factorials, determining prime numbers, generating Fibonacci series, checking Armstrong numbers, verifying leap years, converting temperatures, adding two numbers, printing pyramids, and finding Armstrong numbers within a range. Each program includes user prompts for input and outputs the results accordingly. The programs demonstrate basic shell scripting techniques and arithmetic operations.

Uploaded by

sexybaby6660
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unix Shell Program

The document contains various shell programs that perform different tasks such as checking if a number is even or odd, finding palindromes, calculating factorials, determining prime numbers, generating Fibonacci series, checking Armstrong numbers, verifying leap years, converting temperatures, adding two numbers, printing pyramids, and finding Armstrong numbers within a range. Each program includes user prompts for input and outputs the results accordingly. The programs demonstrate basic shell scripting techniques and arithmetic operations.

Uploaded by

sexybaby6660
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Write a shell program find a number is Even or Odd

echo n "Enter number : "


read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi

Output
Write a shell program to find a number is Palindrome or not
echo “enter the no to check palendrome”
read n
x=0
y=0
m=$n
while [ $n -ne 0 ] do
x=$(( n % 10 ))
y=$(( y * 10 + x ))
n=$(( n / 10 ))
done
echo “y is $y”
echo “number is $m”
if [ $m -eq $y ]
then
echo “value is palendrome”
else
echo “value is not palendrome”
fi

Output
Write a shell program to find a string is palindrome or not

echo Enter the string


read s
echo $s > temp
rvs="$(rev temp)"
if [ $s = $rvs ]
then
echo "it is palindrome"
else
echo " it is not"
fi

Output
Write a shell program to find the factorial of a number

factorial

n=0
on=0
fact=1

echo n "Enter number to find factorial : "


read n

on=$n

while [ $n -ge 1 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done

echo "Factorial for $on is $fact"

Output
Write a shell program to find a number is Prime or not

echo n "Enter a number: "


read num
i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "

Output
Write a shell program to find Fibonacci series

echo "How many number of terms to be generated ?"


read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done

Output
Write a shell program to find a number is Armstrong or not

echo -n "Enter the number: "


read Number
Length=${#Number}
Sum=0
OldNumber=$Number

while [ $Number -ne 0 ]


do
Rem=$((Number%10))
Number=$((Number/10))
Power=$(echo "$Rem ^ $Length" | bc )
Sum=$((Sum+$Power))
done

if [ $Sum -eq $OldNumber ]


then
echo "$OldNumber is an Armstrong number"
else
echo "$OldNumber is not an Armstrong number"
fi

Output
Write a shell program to find a year is leap or not

echo "Enter Year:"


read y
year=$y
y=$(( $y % 4 ))
if [ $y -eq 0 ]
then
echo "$year is Leap Year!"
else
echo "$year is not a Leap Year!"
fi
Output
Write a shell program for Temperature conversion

echo "1. Convert Celsius temperature into Fahrenheit"


echo "2. Convert Fahrenheit temperatures into Celsius"
echo -n "Select your choice (1-2) : "
read choice

if [ $choice -eq 1 ]
then
echo -n "Enter temperature (C) : "
read tc
# formula Tf=(9/5)*Tc+32

tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)


echo "$tc C = $tf F"
elif [ $choice -eq 2 ]
then
echo -n "Enter temperature (F) : "
read tf
# formula Tc=(5/9)*(Tf-32)

tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
echo "$tf = $tc"
else

echo "Please select 1 or 2 only"


exit 1
fi

Output
Write a shell program to add two numbers

echo "Enter the first number"


read a
echo "Enter the second number"
read b
c=$(($a+$b))
echo "sum=$c"

Output
Write a shell program to print a pyramid

#!/bin/bash
Echo “Enter the number”
read count
for((i=1;i<=count;i++))
do
for((j=1;j<=i;j++))
do
echo-n “*”
done
echo
done
exit 0

Output
Write a shell program to find Armstrong number between a
range

#!/bin/bash
i=1
echo “Enter the upper limit”
read n
while((i<=n))
do
c=$i
d=$i
b=0
a=0
while((c>0))
do
a=$((c%10))
b=$((b + a*a*a))
c=$((c/10))
done
if((b==d)); then
echo "$i"
fi
i=$((i+1))
done

output
Write a shell program to print a numeric pyramid

#!/bin/bash number=1
echo "Enter the number" read rows
for((i=1; i<=rows; i++)) do
for((j=1; j<=i; j++))
do
echo -n "$number " number=$((number +
1)) done
number=1 echo
done

Output

You might also like