Читаем 100 Shell Programs in Unix полностью

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’

sum=‘expr $d1 + $d2 + $d3 + $d4 + $d5’

echo Sum of digits = $sum

Sample Run

$sh prg82

Enter any five digit number

12345

Sum of digits = 15

83. If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit was made or loss incurred.

$vi prg83

clear

echo Enter cost price of the item

read cp

echo Enter selling price of the item

read sp

if [ $sp -gt $cp ]

then

echo Seller had made profit

profit=‘echo $sp - $cp | bc’

echo Profit = $profit

else

if [ $cp -gt $sp ]

then

echo Seller has incurred loss

loss=‘echo $cp - $sp | bc’

echo Loss = $loss

else

echo No profit, no loss

fi

fi

Sample Run

$sh prg83

Enter cost price of the item

1500

Enter selling price of the item

2000

Seller had made profit

Profit = 500

84. Write a program to calculate overtime pay of employees. Overtime is paid at the rate of Rs. 12.00 per hour for every hour worked above 40 hours. Assume that employees do not work for fractional part of an hour.

$vi prg84

Clear

Echo How many employees are there?

Read number

emp=1

while [ $emp -le number ]

do

echo enter working hours for employee number $emp

read hours

if [ $hours -gt 40 ]

then

otpay=‘expr \( $hours - 40 \) \* 12’ echo overtime pay = Rs.

$otpay

else

echo no overtime pay

fi

emp=‘expr $emp + 1’

done

Sample Run

$sh prg84

How many employees are there?

5

enter working hours for employee number 1

12

no overtime pay

enter working hours for employee number 2

21

no overtime pay

enter working hours for employee number 3

33

no overtime pay

enter working hours for employee number 4

45

overtime pay = Rs. 60

enter working hours for employee number 5

50

overtime pay = Rs. 120

85. Write a program to generate all combinations of digits 1, 2 and 3 to form different numbers using for loops.

$vi prg85

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

Sample Run

$sh prg85

1 1 1

1 1 2

1 1 3

1 2 1

1 2 2

1 2 3

1 3 1

1 3 2

1 3 3

2 1 1

2 1 2

2 1 3

2 2 1

2 2 2

2 2 3

2 3 1

2 3 2

2 3 3

3 1 1

3 1 2

3 1 3

3 2 1

3 2 2

3 2 3

3 3 1

3 3 2

3 3 3

86. Write a program to check whether a given number is an Armstrong number or not, An Armstrong number is one in which the sum of cube of each of the digits equals that number.

$vi prg86

clear

echo Enter a Number

read n

m=$n

s=0

while [ $n -gt 0 ]

do

q=‘expr $n / 10’

r=‘expr $n - \( $q \* 10 \)’

s=‘expr $s + \( $r \* $r \* $r \)’

n=$q

done

if [ $s -eq $m ]

then

echo The Number Is Armstrong

else

echo The Number Is Not Armstrong

fi

$sh prg86

Enter a Number

153

The Number Is Armstrong

$sh prg86

Enter a Number

152

The Number Is Not Armstrong

87. Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153=(1*1*1)+(5*5*5)+(3*3*3)

$vi prg87

clear

i=1

echo Armstrong numbers are

while [ $i -le 500 ]

do

a=‘echo $i % 10|bc’

b=‘echo $i % 100|bc’

b=‘echo \( $b - $a \) / 10|bc’

c=‘echo $i / 100|bc’

sum=‘echo \( $a \* $a \* $a \) + \( $b \* $b \*

$b \) + \($c \* $c \* $c \)|bc’

if [ $sum -eq $i ]

then

echo $i

fi

i=‘expr $i + 1’

done

Sample Run

$sh prg87

Armstrong numbers are

1

153

370

371

407

88.  Write a program for swapping of two numbers without using any third variable.

$vi prg88

clear

echo enter numbers a and b

read a

read b

b=‘expr $a - $b’

a=‘expr $a - $b’

b=‘expr $a + $b’

echo After Swapping

echo a = $a

echo b = $b

Sample Run

$sh prg88

enter numbers a and b

12

3

After Swapping

a = 3

b = 12

$sh prg88

enter numbers a and b

21

23

After Swapping

a = 23

b = 21

123

89. Program to get pid of the process.

$vi prg89.c

#include

#include

int main()

{

int pid;

pid=getpid();

printf(“The process id of the process is %d\n”,pid);

return 0;

}

Compile

Перейти на страницу:

Похожие книги

Компьютерные сети. 6-е изд.
Компьютерные сети. 6-е изд.

Перед вами шестое издание самой авторитетной книги по современным сетевым технологиям, написанное признанным экспертом Эндрю Таненбаумом в соавторстве со специалистом компании Google Дэвидом Уэзероллом и профессором Чикагского университета Ником Фимстером. Первая версия этого классического труда появилась на свет в далеком 1980 году, и с тех пор каждое издание книги неизменно становилось бестселлером. В книге последовательно изложены основные концепции, определяющие современное состояние компьютерных сетей и тенденции их развития. Авторы подробно объясняют устройство и принципы работы аппаратного и программного обеспечения, рассматривают все аспекты и уровни организации сетей — от физического до прикладного. Изложение теоретических принципов дополняется яркими, показательными примерами функционирования интернета и компьютерных сетей различного типа. Большое внимание уделяется сетевой безопасности. Шестое издание полностью переработано с учетом изменений, произошедших в сфере сетевых технологий за последние годы, и, в частности, освещает такие технологии, как DOCSIS, 4G и 5G, беспроводные сети стандарта 802.11ax, 100-гигабитные сети Ethernet, интернет вещей, современные транспортные протоколы CUBIC TCP, QUIC и BBR, программно-конфигурируемые сети и многое другое.

Дэвид Уэзеролл , Ник Фимстер , Эндрю Таненбаум

Учебные пособия, самоучители