Sie sind auf Seite 1von 7

c  

1) Finding GCD and LCM of two numbers

echo "Gcd Lcm"

echo "enter two numbers for calculating gcd and lcm"

read a1 a2

m1=$a1

m2=$a2

g=0

l=0

r=`expr $a1 % $a2`

while [ $r -ne 0 ]

do

r=`expr $a1 % $a2`

while [ $r -ne 0 ]

do

a1=$a2

a2=$r

r=`expr $a1 % $a2`

done

done

echo "gcd : $a2"

g=`expr $m1 "*" $m2`

l=`expr $g / $a2`

echo "lcm : $l"



c  


output:-

[sunshine@LINUXSERVER ~]$Gcd Lcm

enter two numbers for calculating gcd and lcm

12 10

gcd : 2

lcm : 60

2) To find if the given no is prime or not prime

echo "Prime no"

echo "enter a number"

read n

i=2

j=0

while test $i -lt $n

do

a=`expr $n % $i`

if test $a -eq 0 //2 == 0

then

j=1

break

fi

i=`expr $i + 1`

done

if test $j -eq 1



c  


then

echo "$n is not prime"

else

echo "$n is prime"

fi

output:-

enter a number

31

31 is prime

enter a number

4 is not prime

3) To find all prime no put the the number entered by user

echo "enter the last number you want ?"

read k

n=2

echo "prime nos are : "

while test $n -lt $k

do

i=2



c  


j=0

while test $i -lt $n

do

no=`expr $n % $i`

if test $no -eq 0

then

j=1

break

fi

i=`expr $i + 1`

done

if test $j -ne 1

then

echo "$n"

fi

n=`expr $n + 1`

done

output:

enter the last number you want ?

10

prime nos are :



c  


4)To find factorial of a number

echo "enter a number :"

read no

if test $no -eq 1

then

echo "$no ! = 1"

elif test $no -eq 2

then

echo "$no ! = 2"

else

i=2

ctr=$i

while test $i -le no

do

i=`expr $i + 1`

ctr=`expr $ctr "*" $i`

done

echo "$no ! = $ctr"

fi

output:-

enter a number :

5 ! = 720




c  


5) Fibonacci Series

echo "enter the last number you want ?"

read k

n=0

a=0

b=1

echo "the fibonacci series is : "

echo "0"

echo "1"

k=`expr $k - 2`

while test $n -lt $k

do

no=`expr $a + $b`

echo "$no"

a=$b

b=$no

n=`expr $n + 1`

done

output:

$ sh gcde.txt

enter the last number you want ?

the fibonacci series is :



c