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

1.sum of Digits of A 5 Digit Number

The document contains 15 code snippets demonstrating various programming concepts in the Bash shell scripting language. The snippets include: 1) Summing the digits of a 5-digit number, 2) Converting a decimal number to hexadecimal, 3) Finding the largest of three numbers, 4) A menu-driven script, 5) Printing a multiplication table, 6) Determining if a number is positive or negative, 7) Summing a series of integers, 8) Calculating the factorial of a number, 9) Printing odd numbers between two numbers, 10) Checking if a number is prime, 11) Printing a 4x4 matrix, 12) Printing a chessboard pattern, 13) Sorting numbers into ascending order, 14)

Uploaded by

Rahul Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

1.sum of Digits of A 5 Digit Number

The document contains 15 code snippets demonstrating various programming concepts in the Bash shell scripting language. The snippets include: 1) Summing the digits of a 5-digit number, 2) Converting a decimal number to hexadecimal, 3) Finding the largest of three numbers, 4) A menu-driven script, 5) Printing a multiplication table, 6) Determining if a number is positive or negative, 7) Summing a series of integers, 8) Calculating the factorial of a number, 9) Printing odd numbers between two numbers, 10) Checking if a number is prime, 11) Printing a 4x4 matrix, 12) Printing a chessboard pattern, 13) Sorting numbers into ascending order, 14)

Uploaded by

Rahul Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Sum of digits of a 5 digit number


echo "enter any five digit number"
read num
d1= 'expr $num%10'
num= 'expr $num%10'
d2= 'expr $num%10'
num= 'expr $num%10'
d3= 'expr $num%10'
num= 'expr $num%10'
d4= 'expr $num%10'
num= 'expr $num%10'
d5= 'expr $num%10'
num= 'expr $num%10'
Sum= 'expr $d1+ $d2+ $d3+ $d4+ $d5'
echo "Sum of digits =$ Sum"

2.Decimal to hexadecimal conversion


n=0
nex=0
echo "conversion"
echo "enter the number"
read n
nex= 'echo "0 base =16; I base=10; $n" |bc'
echo "the decimal $n is equivalent to hexadecimal $ nex"

3.Largest of three numbers


echo "enter three number"
read a b c
if test $a – gt $b – a $b – gt $c
then
echo "a is big"
elif
test $b – gt $c
then echo
echo "b is big"
else
echo "c is big"
fi

4.Menu Driven Script


echo "1.Today's date \n" 
echo "2.List of files \n"
echo "3.Users of System \n"
echo "4.Users of processes \n"
echo "5.Quit \n"
echo "enter your choice" 
read ch
case "$ch" in
(1)date ;;
(2)Is – 1 ;;
(3)Who ;;
(4)Ps – f ;;
(5)Exit ;;
5.Mutiplication Table
echo "enter a number"
num=0
read num
if [$num – lt 10]:
then
for(i=1; $i <=10; i++)
do
M= 'expr $i \* $num'
echo "$num * $i= $m"
done
else
for((l=11 ; $l <=20; l++))
do
n= 'expr $l \* $num'
echo "$num * $l = $n"
done
fi

6.Negative or positive number


Op=0
echo "enter a number"
read op
if [ $op –gt 0]
then
echo "the given no is negative number"
elif [$op –eq 0]
then
echo "the given no is neither positive nor negative"
else
echo "this is not a number"
fi

7.Summing of series of integers


Echo "enter the number"
read n
a=0
i=1
while [ $i –le $n]
do
a=$(($a+$i))
((i=$i+1))
done
echo "$a"

8.Factorial of a given Number


n=0
on=0
fact=1
echo-n "enter any number"
read n
on=$n
while [$n – ge1]
do
fact = 'expr $ fact \*$n'
n= 'expr $n -1'
done
echo "factorial for $on is $ fact"

9.Odd Number
Echo –n "enter first no"
Read n1
Echo –n "enter second no"
Read n2
If [ $n2 –gt $n1]
Then
For ((i=$n1 ; i<=$n2 ; i++))
do
test =$ (($ i %2))
if [$test –ne 0];
then
echo $ i
fi
done
else
echo "$n2 must be greater than $ n1"
fi

10.Prime Number
echo "enter the range"
read n
read n1
echo "prime numbers between $n and $n1 are listed below"
until test $n = $n1
do
i=2
m= 'expr $n/2'
r= 'expr $n%$i'
while [$i –le $m –a $r –ne 0]
do
i = 'expr $ i +1'
r = 'expr $ n % $i
done
if [$r – ne 0]
then 
echo "$n"
fi
n = 'expr $n+1'
done

11.Matrix
echo " 4*4 matrix"
for ((i=1; $i<=4; i++))
do
for ((j=1; $j<=4; j++))
echo –n "$j"
done 
echo "ok"
done

12.To Display Chess Board


echo "chess board"
for ((i=1; i<8; i++))
do
sum = 'expr $ i +$j'
emp= 'expr $ sum %2'
if [$emp –eq 0]
then 
echo –e –n " \033 [40 m"
else
echo –e –n "\033 [47 m"
fi
done
echo –e –n "\ 033 [40m"
echo " "
done

13.Asecending Order
echo "enter the number"
for n in 0 1 2 3 4 
do
read S[n]

done
for ((i=0; i<=4; i++))
do
echo " $[S[$i]]"
done
for ((i=0; i<=4; i++))
do
for ((j=0; j<=4; j++))
do
if [$[S[$i]] – gt $[S[$j]] ]
then
temp = $ [S[$i]]
S[$i] = $[S[$j]]
S[$j] = $ temp
fi
done
done
echo "ascending order is"
for ((i=0; i<=4; i++))
do
echo "$[B[$i]]"
done

14.Searching A Number
y=0
echo "enter the elements of array"
for ((i=0; i<5; i++))
do
read n[$i]
done
echo "enter the no. to be searched"
read x
for ((i=0; i<5; i++))
do
if [$x –eq $[n(i)]]
then
y=$ (($y+1))
fi
done
echo "the no $x is present $ y times"

15.Number Triangle
n=0
echo –n "enter any number"
read n
read s
for ((i=1; i<=r ; i++))
do
for ((s=n; s>$i; s--))
do
echo –n " "
done
for ((j=1; j<=$i; j++))
do
echo –n "$i"
done 
echo " "
done 

You might also like