0% found this document useful (0 votes)
178 views20 pages

Fundamental of Computing (FOC)

The document contains several Bash shell programs that demonstrate basic programming concepts like conditionals, loops, functions, input/output. The programs include calculating salary, finding the largest of two numbers, checking leap years, calculating area and perimeter of a rectangle, arithmetic operations using a case statement, printing prime numbers between 1 and 100, printing N natural numbers, calculating the sum of digits in a 5 digit number, calculating factorials, and generating combinations using nested for loops.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views20 pages

Fundamental of Computing (FOC)

The document contains several Bash shell programs that demonstrate basic programming concepts like conditionals, loops, functions, input/output. The programs include calculating salary, finding the largest of two numbers, checking leap years, calculating area and perimeter of a rectangle, arithmetic operations using a case statement, printing prime numbers between 1 and 100, printing N natural numbers, calculating the sum of digits in a 5 digit number, calculating factorials, and generating combinations using nested for loops.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

Program:

/* Basic Salary*/

echo "Basic salary"


read basic
da=` expr $basic \* 10 / 100 `
hra=` expr $basic \* 20 / 100 `
gross=` expr $basic + $da + $hra `
echo "Gross Salary = $gross"
Output:

Basic salary
1000
gross Salary = 1300
Program:

\* To find the biggest of two numbers *\

echo "Enter two nos"


read n m
if [ " $n " -gt " $m " ]
then
echo $n in Big
else
echo $m is big
fi
Output:

Enter two nos


10 20
20 is big
Program:

\* Leap Year *\

echo Enter the year


read y
if [` expr $y % 4 ` -eq "0" ]
then
echo It is leap year
else
echo It is not leap year
fi
Output:

Enter the year


2004
It is leap year

Enter the year


2009
It is not leap year
Program:

\* Area and Perimeter of a Rectangle *\

echo enter length breadth


read length breadth
areaR=` expr $length \* $breadth `
periR=` expr $length \* 2 + $breadth \* 2 `
echo Area of Rectangle= $areaR Perimeter= $periR
Output:

enter length breadth


15 5
Area of Rectangle= 75 Perimeter= 40
Program:

\* Arithmetic Operations Using Case *\

echo "Enter two numbers"


read a b
echo "1.Add 2.Sub 3.Mul 4.Div 5.Exit"
read op
case $op in
1) c= expr $a + $b ;;
2) c= expr $a - $b ;;
3) c= expr $a \* $b ;;
4) c= expr $a / $b ;;
5) exit
esac
echo $c
Output:

Enter two numbers: 2 2


1.Add 2.Sub 3.Mul 4.Div 5.Exit
1
4

Enter two numbers: 4 2


1.Add 2.Sub 3.Mul 4.Div 5.Exit
2
2

Enter two numbers: 4 2


1.Add 2.Sub 3.Mul 4.Div 5.Exit
3
8

Enter two numbers: 4 2


1.Add 2.Sub 3.Mul 4.Div 5.Exit
4
2

Enter two numbers: 4 2


1.Add 2.Sub 3.Mul 4.Div 5.Exit
5
Program:

\* Print Prime nos between 1 and 100 *\

n=1
echo $n
while [ "$n" -le "100" ]
do
i=2
while [ "$i" -le ` expr $n - 1 ` ]
do
if [ ` expr $n % $i ` -eq 0 ]
then
break
else
i=` expr $i + 1 `
fi
done
if [ $n -eq $i ]
then
echo $n
fi
n=` expr $n + 1 `
done
Output:

1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Program:

\* To print N Natural numbers *\

i=1
read n
while [ "$i" -le "$n" ]
do
echo $i
i=` expr $i + 1 `
done
Output:

5
1
2
3
4
5
Program:

\* Sum of digits of five digit numbers *\

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
Output:

Enter any five digit number


12345
sum of digits = 15
Program:

\* Factorial of a given number *\

echo "Enter Number"


read n
i=1
f=1
while [ $i -le $n ]
do
f=` expr $f \* $i `
i=` expr $i + 1 `
done
echo "factorial is.....$f"
Output:

Enter Number
5
factorial is.....120
Program:

\* Combinations of 1, 2 & 3 using for loop *\

clear
for i in 1 2 3
do
for j in 1 2 3
do
for k in 1 2 3
do
echo $i $j $k
done
done
done
Output:

clear
for i in 1 2 3
111 321
112 322
113 323
121 331
122 332
123 333
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313

You might also like