Q2.WAP Accept N-Digit Number and Display It in Words. Answer

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

Q2.WAP accept n-digit number and display it in words.

Answer:
#!/bin/bash
#Exp2.sh

echo " Enter a number of 5 digits "


read num
d=10000
while test $d -ne 0
do
Q=`expr $num / $d`
num=`expr $num % $d`
d=`expr $d / 10`

case $Q in
1) echo " One " ;;
2) echo " Two " ;;
3) echo " Three " ;;
4) echo " Four " ;;
5) echo " Five " ;;
6) echo " Six " ;;
7) echo " Seven " ;;
8) echo " Eight " ;;
9) echo " Nine " ;;
0) echo " Zero " ;;

esac
done

echo "enter two numbers"


read x
read y
echo "enter choice"
Q3.WAP for Calculator.
Answer:
#!/bin/bash
#Exp3.sh

echo "enter two numbers"


read x
read y
echo "enter choice"

echo "MENU"
echo "1. ADD"
echo "2. SUBTRACT"
echo "3. MULTIPLY"
echo "4. DIVIDE"
echo "5. REMAINDER"
echo "6. EXIT"

read case

case $case in
1)a=`expr $x + $y`
echo "$a" ;;
2)a=`expr $x - $y`
echo "$a" ;;
3)a=`expr $x \* $y`
echo "$a" ;;
4)a=`expr $x / $y`
echo "$a" ;;
5)a=`expr $x % $y`
echo "$a" ;;
6)exit
esac

echo “do u want to contine [y/n]:”


read ans
case $ans in
[y/Y]) ./Exp3.sh ;;
[n/N] exit ;;
esac
Q5.WAP to make Sum of Digits
Answer:
#!/bin/bash
#Exp5.sh

echo "enter no"


read n
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Sum of digit for number is $sum"

Q8.Diplay Following Patterns.

1. * 1
** 11
*** 111
**** 1111
***** 11111
Answer:
#!/bin/bash
#Exp8.1.sh

echo "enter the no. of rows to be printed"


read n
for(( i=1; i<=n ; i++ ))
do
for(( j=1 ; j <=i ; j++ ))
do
echo -n "* " Or echo –n “ 1”
done
echo " "
done
2. 1

12
123
1234
12345
Answer:
#!/bin/bash
#Exp8.2.sh

echo "enter the no. of rows to be printed"


read n
for(( i=1; i<=n ; i++ ))
do
for(( j=1 ; j <=i ; j++ ))
do
echo -n "$j "
done
echo " "
done

3. 1

22
333
4444
55555
Answer:
#!/bin/bash
#Exp8.3.sh

echo "enter the no. of rows to be printed"


read n
for(( i=1; i<=n ; i++ ))
do
for(( j=1 ; j <=i ; j++ ))
do
echo -n "$i "
done
echo " "
done
4. 1
2 2
3 3 3
2 2
1
Answer:
#!/bin/bash
#Exp8.4.sh

#!/bin/bash
MAX_NO=0

echo -n "Enter The No Of Rows: "


read MAX_NO

clear

for (( i=1; i<=MAX_NO; i++ ))


do
for (( s=MAX_NO; s>=i; s-- ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " $i"
done
echo ""
done

for (( i=MAX_NO; i>=1; i-- ))


do
for (( s=i; s<=MAX_NO; s++ ))
do
echo -n " "
done
for (( j=1; j<=i; j++ ))
do
echo -n " $i"
done
echo ""
done

5. *
**
***
****
*****
****
***
**
*
Answer:
#!/bin/bash
#Exp8.5.sh

echo "enter the no. of rows to be printed"


read n
for(( i=1; i<=n ; i++ ))
do
for(( j=1 ; j <=i ; j++ ))
do
echo -n "* "
done
echo " "
done
for(( i=n-1 ; i>=1 ; i-- ))
do
for(( j=1 ; j <=i ; j++ ))
do
echo -n "* "
done
echo " "
done
Q9. Write a shell script to display msg according to system time “Good
Morning” “Good Afternoon” “ Good Evening”.
Answer:
#!/bin/bash
#Exp9.sh

while(true)
do
tput clear
tput cup 1 0
x=`date "+ %H:%M:%S"`
echo $x

d=`date "+ %H"`

if test $d -lt 12
then
echo "Good Morning"

elif test $d -lt 16


then
echo "Good Afternoon"

elif test $d -lt 20


then
echo "Good Evening"
else

echo "Good Night"


fi
sleep 1
done
Q10.1.WAP For Prime Number.

Answer:
#!/bin/bash
#Exp10.1.sh

echo "enter the no"


read no
for((i=2;i<no;i++))
do
let rem=$no%$i
if [ $rem -eq 0 ]
then
echo "$no is not a prime no"
exit 1
fi
done
echo "$no is a prime no"

Q10.2.WAP for Fibonnaci series.


Answer:
#!/bin/bash
#Exp10.2.sh

first=0
second=1
echo "enter the length of fibonacci series"
read n
echo "fibonacci series is:"
echo $first
echo $second
for((i=0;i<n-2;i++))
do
let third=$first+$second
first=$second
second=$third
echo $third
done
Q10.3.WAP For Factorial.

Answer:
#!/bin/bash
#Exp10.2.sh

echo "enter a number:"


read x
fact=1
for(( i=1 ;i<=x ; i++))
do
fact=`expr $fact \* $i`
done
echo "the factorial of $x is $fact"

11. WAP to except a character from a user, check whether it has


uppercase, lowercase, digit or special case
Answer:
#!/bin/bash
#Exp11.sh

echo"Enter a character"
read c
lc=0
uc=0
sc=0
case $c in
[a-z]) echo "It is lowercase letter";;
lc = `expr $lc + 1`
[A-Z]) echo "It is uppercase letter";;
[0-9]) echo "It is a number";;
?) echo "It is a special case letter";;
esac
Q12.Script To Check the Content Of Two Files For Similarities And If
Same Then Delete Second File
Answer:
#!/bin/bash
#Exp12.sh

if (cmp $1,$2)
echo "Content of the file are same"
rm $2
else
echo "Content of the file are different"
fi

Q14.Write A program For Leap year.


Answer:
#!/bin/bash
#Exp14.sh

echo "enter year"


read x
y=$(($x%4))
if [ $y -eq 0 ] ; then
echo "the year is a leap year"
else
echo "the year is not a leap year"
fi

Q16.WASS to sort no without using Sort command


Answer:
#!/bin/bash
#Exp16.sh

printf "Enter Number Of Elements To Be Sorted : "


read n

declare arr[$n]

echo "Enter The Numbers : "


for((k=0;k<n;k++))
do
let b=$k+1
printf "\tNumber $b : "
read arr[$k]
done

for((i=0;i<$n-1;i++))
do
for((j=$i+1;j<$n;j++))
do
if [ ${arr[$i]} -gt ${arr[$j]} ];then
let temp=${arr[$i]}
let arr[$i]=${arr[$j]}
let arr[$j]=$temp
fi
done
done
printf "Sorted List Is : "
for((k=0;k<n;k++))
do
printf "${arr[$k]} "
done

You might also like