Powered By Blogger

Saturday, May 21, 2011

            My Great Web page                  

Friday, March 18, 2011

Program to multiply two polynomials in c with array .

/* Program to multiply two polynomials. */

/#include
#include

#define MAX 10

struct term
{
int coeff ;
int exp ;
} ;

struct poly
{
struct term t [10] ;
int noofterms ;
} ;

void initpoly ( struct poly *) ;
void polyappend ( struct poly *, int, int ) ;
struct poly polyadd ( struct poly, struct poly ) ;
struct poly polymul ( struct poly, struct poly ) ;
void display ( struct poly ) ;

void main( )
{
struct poly p1, p2, p3 ;

clrscr( ) ;

initpoly ( &p1 ) ;
initpoly ( &p2 ) ;
initpoly ( &p3 ) ;

polyappend ( &p1, 1, 4 ) ;
polyappend ( &p1, 2, 3 ) ;
polyappend ( &p1, 2, 2 ) ;
polyappend ( &p1, 2, 1 ) ;

polyappend ( &p2, 2, 3 ) ;
polyappend ( &p2, 3, 2 ) ;
polyappend ( &p2, 4, 1 ) ;

p3 = polymul ( p1, p2 ) ;

printf ( "\nFirst polynomial:\n" ) ;
display ( p1 ) ;

printf ( "\n\nSecond polynomial:\n" ) ;
display ( p2 ) ;

printf ( "\n\nResultant polynomial:\n" ) ;
display ( p3 ) ;

getch( ) ;
}

/* initializes elements of struct poly */
void initpoly ( struct poly *p )
{
int i ;
p -> noofterms = 0 ;
for ( i = 0 ; i < MAX ; i++ )
{
p -> t[i].coeff = 0 ;
p -> t[i].exp = 0 ;
}
}

/* adds the term of polynomial to the array t */
void polyappend ( struct poly *p, int c, int e )
{
p -> t[p -> noofterms].coeff = c ;
p -> t[p -> noofterms].exp = e ;
( p -> noofterms ) ++ ;
}

/* displays the polynomial equation */
void display ( struct poly p )
{
int flag = 0, i ;
for ( i = 0 ; i < p.noofterms ; i++ )
{
if ( p.t[i].exp != 0 )
printf ( "%d x^%d + ", p.t[i].coeff, p.t[i].exp ) ;
else
{
printf ( "%d", p.t[i].coeff ) ;
flag = 1 ;
}
}
if ( !flag )
printf ( "\b\b " ) ;

}
/* adds two polynomials p1 and p2 */
struct poly polyadd ( struct poly p1, struct poly p2 )
{
int i, j, c ;
struct poly p3 ;
initpoly ( &p3 ) ;

if ( p1.noofterms > p2.noofterms )
c = p1.noofterms ;
else
c = p2.noofterms ;

for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ )
{
if ( p1.t[i].coeff == 0 && p2.t[j].coeff == 0 )
break ;
if ( p1.t[i].exp >= p2.t[j].exp )
{
if ( p1.t[i].exp == p2.t[j].exp )
{
p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;
p3.t[p3.noofterms].exp = p1.t[i].exp ;
i++ ;
j++ ;
}
else
{
p3.t[p3.noofterms].coeff = p1.t[i].coeff ;
p3.t[p3.noofterms].exp = p1.t[i].exp ;
i++ ;
}
}
else
{
p3.t[p3.noofterms].coeff = p2.t[j].coeff ;
p3.t[p3.noofterms].exp = p2.t[j].exp ;
j++ ;
}
}
return p3 ;
}

/* multiplies two polynomials p1 and p2 */
struct poly polymul ( struct poly p1, struct poly p2 )
{
int coeff, exp ;
struct poly temp, p3 ;

initpoly ( &temp ) ;
initpoly ( &p3 ) ;

if ( p1.noofterms != 0 && p2.noofterms != 0 )
{
int i ;
for ( i = 0 ; i < p1.noofterms ; i++ )
{
int j ;

struct poly p ;
initpoly ( &p ) ;

for ( j = 0 ; j < p2.noofterms ; j++ )
{
coeff = p1.t[i].coeff * p2.t[j].coeff ;
exp = p1.t[i].exp + p2.t[j].exp ;
polyappend ( &p, coeff, exp ) ;
}

if ( i != 0 )
{
p3 = polyadd ( temp, p ) ;
temp = p3 ;
}
else
temp = p ;
}
}
return p3 ;
}

Program to add two polynomials in c

/*: Program to add two polynomials. */

#include "stdio.h"
#include "conio.h"
#define MAX 10

struct term
{
int coeff ;
int exp ;
} ;

struct poly
{
struct term t [10] ;
int noofterms ;
} ;


void initpoly ( struct poly * ) ;
void polyappend ( struct poly *, int c, int e ) ;
struct poly polyadd ( struct poly, struct poly ) ;
void display ( struct poly ) ;

void main( )
{
struct poly p1, p2, p3 ;

clrscr( ) ;

initpoly ( &p1 ) ;
initpoly ( &p2 ) ;
initpoly ( &p3 ) ;

polyappend ( &p1, 1, 7 ) ;
polyappend ( &p1, 2, 6 ) ;
polyappend ( &p1, 3, 5 ) ;
polyappend ( &p1, 4, 4 ) ;
polyappend ( &p1, 5, 2 ) ;

polyappend ( &p2, 1, 4 ) ;
polyappend ( &p2, 1, 3 ) ;
polyappend ( &p2, 1, 2 ) ;
polyappend ( &p2, 1, 1 ) ;
polyappend ( &p2, 2, 0 ) ;

p3 = polyadd ( p1, p2 ) ;

printf ( "\nFirst polynomial:\n" ) ;
display ( p1 ) ;

printf ( "\n\nSecond polynomial:\n" ) ;
display ( p2 ) ;

printf ( "\n\nResultant polynomial:\n" ) ;
display ( p3 ) ;

getch( ) ;
}

/* initializes elements of struct poly */
void initpoly ( struct poly *p )
{
int i ;
p -> noofterms = 0 ;
for ( i = 0 ; i <>
{
p -> t[i].coeff = 0 ;
p -> t[i].exp = 0 ;
}
}

/* adds the term of polynomial to the array t */
void polyappend ( struct poly *p, int c, int e )
{
p -> t[p -> noofterms].coeff = c ;
p -> t[p -> noofterms].exp = e ;
( p -> noofterms ) ++ ;
}

/* displays the polynomial equation */
void display ( struct poly p )
{
int flag = 0, i ;
for ( i = 0 ; i <>
{
if ( p.t[i].exp != 0 )
printf ( "%d x^%d + ", p.t[i].coeff, p.t[i].exp ) ;
else
{
printf ( "%d", p.t[i].coeff ) ;
flag = 1 ;
}
}
if ( !flag )
printf ( "\b\b " ) ;

}

/* adds two polynomials p1 and p2 */
struct poly polyadd ( struct poly p1, struct poly p2 )
{
int i, j, c ;
struct poly p3 ;
initpoly ( &p3 ) ;

if ( p1.noofterms > p2.noofterms )
c = p1.noofterms ;
else
c = p2.noofterms ;

for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ )
{
if ( p1.t[i].coeff == 0 && p2.t[j].coeff == 0 )
break ;
if ( p1.t[i].exp >= p2.t[j].exp )
{
if ( p1.t[i].exp == p2.t[j].exp )
{
p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;
p3.t[p3.noofterms].exp = p1.t[i].exp ;
i++ ;
j++ ;
}
else
{
p3.t[p3.noofterms].coeff = p1.t[i].coeff ;
p3.t[p3.noofterms].exp = p1.t[i].exp ;
i++ ;
}
}
else
{
p3.t[p3.noofterms].coeff = p2.t[j].coeff ;
p3.t[p3.noofterms].exp = p2.t[j].exp ;
j++ ;
}
}
return p3 ;
}

ARRAY IN C

/* Program to implement an array. */















#include "conio.h"
#include "stdio.h"

#define MAX 5

void insert ( int *, int pos, int num ) ;
void del ( int *, int pos ) ;
void reverse ( int * ) ;
void display ( int * ) ;
void search ( int *, int num ) ;

void main( )
{
int arr[5] ;

clrscr( ) ;

insert ( arr, 1, 11 ) ;
insert ( arr, 2, 12 ) ;
insert ( arr, 3, 13 ) ;
insert ( arr, 4, 14 ) ;
insert ( arr, 5, 15 ) ;

printf ( "\nElements of Array: " ) ;
display ( arr ) ;

del ( arr, 5 ) ;
del ( arr, 2 ) ;
printf ( "\n\nAfter deletion: " ) ;
display ( arr ) ;

insert ( arr, 2, 222 ) ;
insert ( arr, 5, 555 ) ;
printf ( "\n\nAfter insertion: " ) ;
display ( arr ) ;
reverse ( arr ) ;
printf ( "\n\nAfter reversing: " ) ;
display ( arr ) ;
search ( arr, 222 ) ;
search ( arr, 666 ) ;

getch( ) ;
}

/* inserts an element num at given position pos */
void insert ( int *arr, int pos, int num )
{
/* shift elements to right */
int i ;
for ( i = MAX - 1 ; i >= pos ; i-- )
arr[i] = arr[i - 1] ;
arr[i] = num ;
}

/* deletes an element from the given position pos */
void del ( int *arr, int pos )
{
/* skip to the desired position */
int i ;
for ( i = pos ; i <>
arr[i - 1] = arr[i] ;
arr[i - 1] = 0 ;
}

/* reverses the entire array */
void reverse ( int *arr )
{
int i ;
for ( i = 0 ; i <>
{
int temp = arr[i] ;
arr[i] = arr[MAX - 1 - i] ;
arr[MAX - 1 - i] = temp ;
}
}

/* searches array for a given element num */
void search ( int *arr, int num )
{
/* Traverse the array */
int i ;
for ( i = 0 ; i <>
{
if ( arr[i] == num )
{
printf ( "\n\nThe element %d is present at %dth position.", num,
i + 1 ) ;
return ;
}
}

if ( i == MAX )
printf ( "\n\nThe element %d is not present in the array.", num ) ;
}

/* displays the contents of a array */
void display ( int *arr )
{
/* traverse the entire array */
int i ;
printf ( "\n" ) ;
for ( i = 0 ; i <>
printf ( "%d\t", arr[i] ) ;
}

Thursday, March 17, 2011

ENGLISH WORDS 1

ENGLISH WORDS

1- AARONIC (ADJ) - pertaining to priesthood .

2- AASVOGEL (N) - S African vulture .

3- ABACA (N) - plantain grown in the Philippine island .

4- ABACK (ADJ) - taken by surprise .

5- ABADDON (N) - hell .

6- ABANDON (V) - to give up .

7- ABASE (V)- to degrade .

8- ABASH (V)- to strike with shame or embarrassment .

9- ABATE (V)- to subside, to lessen or reduce, to mitigate .

10- ABBEY (N) - A convent under an abbot or abbess .

UNIX PRACTICALS

§ AIM :-

1. This script will print the fibonacci series upto given upper limit.

§ PROGRAM CODE:-

read -p "Enter the upper limit " n

a=0

b=1

c=`expr $a + $b`

echo -n "$c "

i=1

while [ $c -le $n ]

do

echo -n "$c "

a=$b

b=$c

c=`expr $a + $b`

i=`expr $i + 1`

done

§ OUTPUT:-

[study@localhost ~]$ sh fibonacci

Enter the upper limit 40

1 1 2 3 5 8 13 21 34

§ DISCUSSION:-

We can generate Fibonacci numbers as follows:

f(n) = f(n-1) + f(n-2)

Then the series Fibonacci numbers within the upper 10 limit will be

1 1 2 3 5 8

---------------------------------

§ AIM:-

1. This shell script program is to find the sum of digits of a given number and to reverse the digits of that number.

§ PROGRAM CODE:-

read -p "Enter a number " n

sum=0

revnum=""

while [ $n -gt 0 ]

do

d=`expr $n % 10`

n=`expr $n / 10`

sum=`expr $sum + $d`

revnum=$revnum$d

done

echo "The reverse number is $revnum"

echo "The sum of the digit is $sum"

§ OUTPUT:-

[study@localhost ~]$ sh sumdigit.sh

Enter a number 1234

The reverse number is 4321

The sum of the digit is 10

[study@localhost ~]$ sh sumdigit.sh

Enter a number 4682

The reverse number is 2864

The sum of the digit is 20

§ DISCUSSION:-

Check the number whether the number is valid integer or not.Check the number whether the number is greater than zero then take the remainder of modulo 10.Divide the number by 10.Then update the sum variable by adding the remainder and update the varuable revnum by concatinating the remainder.finally the sum variable contain the sum of digits and revnum contains reverse of the number.

-------------------------------------

§ AIM:-

2. This shell script program is to check whether a given number is prime or not.

§ PROGRAM CODE:-

echo -e "Enter a number:- \c"

read n

d=`factor $n|wc -w`

if [ $d -gt 2 ]

then

echo "nonprime"

else

echo prime

fi

§ OUTPUT:-

[study@localhost ~]$ sh prime.sh

Enter a number:- 7

prime

[study@localhost ~]$ sh prime.sh

Enter a number:- 6

nonprime

[study@localhost ~]$ sh prime.sh

Enter a number:- 28

nonprime

[study@localhost ~]$ sh prime.sh

Enter a number:- 19

prime

§ DISCUSSION:-

The number is prime if its divide by 1 and that number i.e. it's exactly have two factors then the number is prime otherwise it is non prime.This is done by factor and wc -w command.It also be done by using division or modulo division operator.

------------------------------------------


§ AIM:-

3. This shell script program is to see the even and odd number between a given range.

§ PROGRAM CODE:-

read -p "Enter the lower limit " l

read -p " Enter the upper limit " u

for (( i=l ; i <= u ; i ++ ))

do

d=`expr $i % 2`

if [ $d -eq 0 ]

then

echo "$i--------even"

else

echo "$i--------odd"

fi

done

§ OUTPUT:-

[study@localhost ~]$ sh even.sh

Enter the lower limit 10

Enter the upper limit 20

10--------even

11--------odd

12--------even

13--------odd

14--------even

15--------odd

16--------even

17--------odd

18--------even

19--------odd

20--------even

§ DISCUSSION:-

This program checks the number within the given range is even or odd by calculating remainder of the modulo 2 division of that number.It uses expr command to evaluate this.

---------------------------

§ AIM:-

4. This script will find out the sum of the following series -

1!+2!+3!+--------+n!

where n is the given number.

§ PROGRAM CODE:-

read -p "Enter the upper limit" n

sum=0

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

do

d=1

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

do

d=`expr $d \* $k`

done

sum=`expr $sum + $d`

done

echo "sum of factorials is $sum "

§ OUTPUT:-

[study@localhost ~]$ sh factsum.sh

Enter the upper limit6

sum of factorials is 873

[study@localhost ~]$ sh factsum.sh

Enter the upper limit 5

sum of factorials is 153

[study@localhost ~]$ sh factsum.sh

Enter the upper limit 4

sum of factorials is 33

§ DISCUSSION:-

This program calculate the sum of the factorials upto the given number by using two for loop and expr command.The inner loop calculates the factorial of each number and then the sum variable is updated.

---------------------------------------


§ AIM:-

5. This shell script program will print the following pattern-

1

2 3

3 4 5

4 5 6 7

§ PROGRAM CODE:-

read -p "Enter the no. of rows" n

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

do

for (( k=0 ; k <>

do

d=`expr $k + $i`

echo -n "$d "

done

echo " "

done

§ OUTPUT:-

[study@localhost ~]$ sh series1.sh

Enter the no. of rows7

1

2 3

3 4 5

4 5 6 7

5 6 7 8 9

6 7 8 9 10 11

7 8 9 10 11 12 13

§ DISCUSSION:-

Here the given series of numbers are generated using for loop.

--------------------------------------------

§ AIM:-

6. This recursive shell script program is to calculate X^Y where both X and Y are non negetive integers.

§ PROGRAM CODE:-

function func()

{

n=$1

m=$2

if [ $m -eq 0 ]

then

echo 1

else

m=`expr $m - 1`

s=`func $n $m`

d=`expr $n \* $s`

echo $d

fi

}

read -p "enter a number" x

read -p "enter the power of the no. " y

r=`func $x $y`

echo $r

§ OUTPUT:-

[study@localhost ~]$ sh power.sh

enter a number 3

enter the power of the no. 2

9

[study@localhost ~]$ sh power.sh

enter a number 5

enter the power of the no. 5

3125

[study@localhost ~]$ sh power.sh

enter a number 4

enter the power of the no. 2

16

§ DISCUSSION:-

The expression X to the power Y (X^Y) is calculated as follows:

X^Y = X * func (Y-1)

This program calculates X^Y using the function func() in recursive way.

------------------------------------

§ AIM:-

7. This shell script program is to calculate G.C.D. of two non negetive integer.

§ PROGRAM CODE:-

function gcd_2no()

{

nx=$1

my=$2

r=`expr $nx % $my`

t=$my

my=`expr $nx % $my`

nx=$t

if [ $r -eq 0 ]

then

echo $nx

else

gcd_2no $nx $my

fi

}

read -p "enter first number : " x

read -p "enter second number : " y

g=`gcd_2no $x $y`

echo $g

§ OUTPUT:-

[study@localhost ~]$ sh gcd.sh

enter first number : 6

enter second number : 21

3

[study@localhost ~]$ sh gcd.sh

enter first number : 8

enter second number : 2

2

§ DISCUSSION:-

This script calculates the G.C.D of two non negetive numbers by using recursive function.

§ AIM:-

8. This menu driven sheel script program will find out addition,subtraction,multiplication and division of two positive numbers.The calculations are to be performed in seperate function.

§ PROGRAM CODE:-

function addition()

{

t=$1

p=$2

k=`expr $t + $p`

echo $k

}

function subtraction()

{

t1=$1

p1=$2

k1=`expr $t1 - $p1`

echo $k1

}

function multiplication()

{

t2=$1

p2=$2

k2=`expr $t2 \* $p2`

echo $k2

}

function division()

{

t3=$1

p3=$2

k3=`expr $t3 / $p3`

echo $k3

}

while true

do

echo "enter the 1st +ve no. : "

read a

echo "enter the 2nd +ve no. : "

read b

clear

echo " 1> ADDITION "

echo " 2> SUBTRACTION "

echo " 3> MULTIPLICATION "

echo " 4> DIVISION "

echo " 5> EXIT "

read -p "ENTER YOUR CHOICE " n

case $n in

1)

r=`addition $a $b`

echo -n "sum = "

echo $r

;;

2)

r1=`subtraction $a $b`

echo -n "subtraction = "

echo $r1

;;

3)

r2=`multiplication $a $b`

echo -n "multiplication = "

echo $r2

;;

4)

r3=`division $a $b`

echo -n "division = "

echo $r3

;;

5)

echo End of the program

exit

;;

esac

done

§ OUTPUT:-

[study@localhost ~]$ sh menu.sh

enter the 1st +ve no. :

6

enter the 2nd +ve no. :

3

1> ADDITION

2> SUBTRACTION

3> MULTIPLICATION

4> DIVISION

5> EXIT

ENTER YOUR CHOICE 1

sum = 9

enter the 1st +ve no. :

4

enter the 2nd +ve no. :

3

1> ADDITION

2> SUBTRACTION

3> MULTIPLICATION

4> DIVISION

5> EXIT

ENTER YOUR CHOICE 3

multiplication = 12

enter the 1st +ve no. :

10

enter the 2nd +ve no. :

4

1> ADDITION

2> SUBTRACTION

3> MULTIPLICATION

4> DIVISION

5> EXIT

ENTER YOUR CHOICE 2

subtraction = 6

enter the 1st +ve no. :

15

enter the 2nd +ve no. :

3

1> ADDITION

2> SUBTRACTION

3> MULTIPLICATION

4> DIVISION

5> EXIT

ENTER YOUR CHOICE 4

division = 5

1> ADDITION

2> SUBTRACTION

3> MULTIPLICATION

4> DIVISION

5> EXIT

ENTER YOUR CHOICE 5

End of the program

§ DISCUSSION:-

This menu driven program takes two positive numbers as input.Then it calculates addition, subtraction, multiplication and division as per user choice using switch case.The EXIT function is to come out from the program.

§ AIM:-

This shell script program is to find an number within an array of elements using linear search method.

§ PROGRAM CODE:-

clear

read -p "Enter the number of elements:- " n

for (( i=0 ; i <>

do

read -p "Enter an element: " a[$i]

done

read -p "Enter the number to be searched:- " m

for (( i=0 ; i <>

do

if [ $m -eq ${a[$i]} ]

then

t=`expr $i + 1`

echo $m is present at position $t

exit

fi

done

echo $m is not in the array

§ OUTPUT:-

[study@localhost ~]$ sh lsearch.sh

Enter the number of elements:- 5

Enter an element: 11

Enter an element: 22

Enter an element: 33

Enter an element: 44

Enter an element: 55

Enter the number to be searched:- 33

33 is present at position 3

[study@localhost ~]$ sh lsearch.sh

Enter the number of elements:- 5

Enter an element: 11

Enter an element: 22

Enter an element: 33

Enter an element: 44

Enter an element: 55

Enter the number to be searched:- 66

66 is not in the array

§ DISCUSSION:-

The no. of elements of the array is provided by the user.This searching technique uses lenear search method.It can also be done by using binary search method.If the searched number is present in the array then it shows the position of the number in the array,if it is not present in the array then it shows a switable message.

§ AIM:-

This shell script program is to find the minimum of n numbers present in an array.

§ PROGRAM CODE:-

clear

read -p "Enter the no. of elements: " n

for (( i=0 ; i <>

do

read -p "Enter an element: " a[$i]

done

min=${a[0]}

for (( i=1 ; i <>

do

if [ $min -gt ${a[$i]} ]

then

min=${a[$i]}

fi

done

echo The minimum number is $min

§ OUTPUT:-

[study@localhost ~]$ sh minnoarry.sh

Enter the no. of elements: 5

Enter an element: 66

Enter an element: 44

Enter an element: 11

Enter an element: 77

Enter an element: 88

The minimum number is 11

§ DISCUSSION:-

Here we find the minimum number among the set of elements within the array.The array of elements are taken input from user,then finds the minimum number among them.It is done by using for loop and -gt command.


§ AIM:-

This shell script program is to sort n number of elements present in an array in ascending order.

§ PROGRAM CODE:-

read -p "Enter the no. of elements : " n

for (( i=0 ; i <>

do

read -p "Enter an element : " a[$i]

done

for (( i=0 ; i < `expr $n - 1` ; i ++ ))

do

for (( k=`expr $i + 1` ; k <>

do

if [ ${a[$i]} -gt ${a[$k]} ]

then

t=${a[$i]}

a[$i]=${a[$k]}

a[$k]=$t

fi

done

done

echo The sorted elements are :

echo ${a[*]}

§ OUTPUT:-

[study@localhost ~]$ sh selectnsrt.sh

Enter the no. of elements : 5

Enter an element : 55

Enter an element : 11

Enter an element : 99

Enter an element : 33

Enter an element : 22

The sorted elements are :

11 22 33 55 99

[study@localhost ~]$ sh selectnsrt.sh

Enter the no. of elements : 10

Enter an element : 16

Enter an element : 67

Enter an element : 89

Enter an element : 34

Enter an element : 44

Enter an element : 50

Enter an element : 25

Enter an element : 45

Enter an element : 99

Enter an element : 13

The sorted elements are :

13 16 25 34 44 45 50 67 89 99

§ DISCUSSION:-

This program is implemented by using selection sort technique.It uses for loop and swapping of two numbers is done by using third variable.This program can also be done by redirecting the array elements into a file then sort the whole file using sort command or any kind of sorting method can be used.


§ AIM:-

9. This shell script program is to test a string whether pallindrome or not.

§ PROGRAM CODE:-

read -p "Enter a string " str

revstr=""

l=`echo $str|wc -c`

l=`expr $l - 1`

i=1

while [ $i -le $l ]

do

c=`echo $str|cut -c $i`

revstr=$c$revstr

i=`expr $i + 1`

done

if [ "$str" = "$revstr" ]

then

echo pallindrome

else

echo not pallindrome

fi

§ OUTPUT:-

[study@localhost ~]$ sh palindrm.sh

Enter a string madam

pallindrome

[study@localhost ~]$ sh palindrm.sh

Enter a string study

not pallindrome

§ DISCUSSION:-

The above program will take a input string.Count the length of the string and cut the alphabet from the string then padded into a variable reversely.So this variable containing the reverse of the string.If the reverse of the string and the original string are equal then the string is pallindrome else not.


§ AIM:-

10. Wrie a shell script to find all prime factor of a given number.

§ PROGRAM CODE:-

read -p "Enter the number :- " n

touch temp.dat

rm temp.dat

for i in `factor $n|cut -d ":" -f2`

do

echo $i>>temp.dat

done

sort -n temp.dat|uniq

§ OUTPUT:-

[study@localhost ~]$ sh factor.sh

Enter the number :- 28

2

7

[study@localhost ~]$ sh factor.sh

Enter the number :- 8

2

[study@localhost ~]$ sh factor.sh

Enter the number :- 15

3

5

§ DISCUSSION:-

The prime factors of a number are deduced by the command factor.Then cut each factor using cut command and redirect into a file.The file will appand the value for each 'i'.The command sort -n will sort the whole file into ascending order.The uniq command checks the uniqueness of the numbers in the file.

§ AIM:-

This shell script program is to check the type of the input file name.

§ PROGRAM CODE:-

echo -e "Enter the file name : \c"

read file

if [ -s $file ]

then

echo "Size Greaterthan Zero"

fi

if [ -x $file ]

then

echo "The file is a executable file!!"

fi

if [ -f $file ]

then

echo "This is a ordinary file!!"

fi

if [ -d $file ]

then

echo "The file is a directory!!"

fi

if [ -c $file ]

then

echo "This is a character special file!!"

fi

if [ -r $file ]

then

echo "Read permission is set!!"

fi

if [ -w $file ]

then

echo "Write permission is set!!"

fi

if [ -k $file ]

then

echo "Sticky bit is set!!"

fi

if [ -b $file ]

then

echo "This is a system file!!"

fi

§ OUTPUT:-

[study@localhost ~]$ sh ftype.sh

Enter the file name : d1

Size Greaterthan Zero

The file is a executable file!!

The file is a directory!!

Read permission is set!!

Write permission is set!!

[study@localhost ~]$ sh ftype.sh

Enter the file name : student.dat

Size Greaterthan Zero

This is a ordinary file!!

Read permission is set!!

Write permission is set!!

§ DISCUSSION:-

This script will check the type of the given file whether it is executable file or ordinary file or charecter special file or system file or directry by using the command -x,-f,-c,-b,-d respectively and checks whether it has read permission or write permission using the command -r,-w.It also checks whether it has size greater than zero or not using -s command.-k command checks it's sticky bit is set or not.




§ AIM:-

Write a shell script program a forward slash "/" to a directry name,a star "*" to an executable file name of files of a directry.The directry name is supplied as command line argument.If the directry name is not supplied as command line argument,current directry will be assumed.

§ PROGRAM CODE:-

for i in `ls $1`

do

fname=$1/$i

if [ -d $fname ]

then

mv $fname $fname"\/"

else

if [ -x $fname ]

then

mv $fname $fname"*"

fi

fi

done

§ OUTPUT:-

[study@localhost d1]$ ls -l

total 24

drwxrwxr-x 2 study study 4096 2010-01-04 23:06 d2

drwxrwxr-x 2 study study 4096 2010-01-04 23:06 d3

-rw-rw-r-- 1 study study 17 2010-01-04 23:07 f1

-rw-rw-r-- 1 study study 16 2010-01-04 23:09 f2

-rwxrw-r-- 1 study study 16 2010-01-04 23:10 f3

-rwxrw-r-- 1 study study 16 2010-01-04 23:10 f4

[study@localhost ~]$ sh directry.sh d1

[study@localhost d1]$ ls -l

total 24

drwxrwxr-x 2 study study 4096 2010-01-04 23:06 d2\

drwxrwxr-x 2 study study 4096 2010-01-04 23:06 d3\

-rw-rw-r-- 1 study study 17 2010-01-04 23:07 f1

-rw-rw-r-- 1 study study 16 2010-01-04 23:09 f2

-rwxrw-r-- 1 study study 16 2010-01-04 23:10 f3*

-rwxrw-r-- 1 study study 16 2010-01-04 23:10 f4*

§ DISCUSSION:-

This program takes the directry name as command line argument.The command -d checks whether the subdirectry and files under that directry is directry or not and -x checks the execution permission of the files under that directry.


§ AIM:-

This shell script program is to calculate the factorial of a given number using recursive function.

§ PROGRAM CODE:-

function fact()

{

n=$1

if [ $n -eq 0 -o $n -eq 1 ]

then

echo 1

else

t=$n

n=`expr $n - 1`

r=`fact $n`

s=`expr $t \* $r`

echo $s

fi

}

read -p "Enter the number:- " n

p=`fact $n`

echo "the factorial is $p"

§ OUTPUT:-

[study@localhost ~]$ sh factrec.sh

Enter the number:- 3

the factorial is 6

[study@localhost ~]$ sh factrec.sh

Enter the number:- 5

the factorial is 120

[study@localhost ~]$ sh factrec.sh

Enter the number:- 4

the factorial is 24

§ DISCUSSION:-

The factorial of a number (n!) means as following:

n! = n * (n - 1) * (n - 2) * ..... * 1

I n this shell script we calculate the factorial of a number by using recursive function.

------------------------------


§ AIM:-

Weite a shell script that will search for a given pattern in a text file,both of which must be supplied as a command line argument to the script.The pattern may appear anywhere in the text file and show all possible instance,such as at the beginning,at the end of the line,as a substring etc.

§ PROGRAM CODE:-

grep "$1" $2 >/dev/null

if [ $? -eq 0 ]

then

echo "At the begging of the lines:-"

echo "------------------------------------"

grep "^$1" $2

echo "------------------------------------"

echo "At the end of the lines:-"

echo "------------------------------------"

grep "$1$" $2

echo "------------------------------------"

echo "As substrings:-"

echo "------------------------------------"

grep -v "^$1" $2 | grep -v "$1$" | grep "$1"

echo "------------------------------------"

else

echo "not valid"

fi

§ OUTPUT:-

[study@localhost ~]$ sh pattern.sh student stu.dat

At the begging of the lines:-

------------------------------------

student is very noughty.

------------------------------------

At the end of the lines:-

------------------------------------

he is very good student

------------------------------------

As substrings:-

------------------------------------

ram is a student of class v.

those boys are the student of precidency college.

------------------------------------

§ DISCUSSION:-

Here the pattern "student" and file name stu.dat are supplied as command line argument.The valid sentences are checked using environment variable then finds the sentences containing the pattern at the beginning, at the end of the line,as a substring etc. using grep command.


§ AIM:-

This shell script program is to rename a group of a files for example,rename all files where file names end with htm so that they end with html.

§ PROGRAM CODE:-

echo Files with extension .htm are

ls *.htm

for i in `ls *htm`

do

pname=`echo $i|cut -d "." -f1`

newname=$pname".html"

mv $i $newname

done

echo The converted files are

ls *.html

§ OUTPUT:-

[study@localhost shell.prog]$ sh convert

Files with extension .htm are

f1.htm f2.htm f3.htm f4.htm

The converted files are

f1.html f2.htm f3.htm f4.htm

§ DISCUSSION:-

The above program will convert the extension 'htm' to 'html'.*.htm will retrieve all the files which have .htm extension.The cut command cut the base file name of those file who have extension '.htm' and then the 'mv' command will rename the file and correct the extension by 'html'.Thus the files are renamed by the .html extension.

§ AIM:-

Write a shell script which print the text file in reverse way i.e the last line becomes the frist line,second last line becomes second line.....the frist becomes the last one.

§ PROGRAM CODE:-

echo "The original file $1 is:-"

echo "---------------------------------------"

cat $1

echo "---------------------------------------"

echo The reverse of that file is:-

echo "---------------------------------------"

lines=`wc -l $1`

l=`echo $lines|cut -d " " -f1`

i=$l

while [ $i -ge 1 ]

do

line=`head -n $i $1|tail -n 1`

echo $line

i=`expr $i - 1`

done

§ OUTPUT:-

[study@localhost ~]$ sh revfile.sh linux1

The original file linux1 is:-

---------------------------------------

The Linux is an operating system

I am using Mandrake

Mandrake is a version of Linux

Mandrake has a great Graphics

Linux have the high security

---------------------------------------

The reverse of that file is:-

---------------------------------------

Linux have the high security

Mandrake has a great Graphics

Mandrake is a version of Linux

I am using Mandrake

The Linux is an operating system

§ DISCUSSION:-

The file name is given by command line argument.cat shows the content of the original file.Then it counts the no. of lines of the file and print each line from last to first using head and tail command.Thus the content of the file is printed reversly.this program can also be done using tac command directly.


§ AIM:-

This shell script program is to find out the vowel frequency of a given string.

§ PROGRAM CODE:-

echo -e "Enter the string \c"

read str

l=`echo $str|wc -c`

l=`expr $l - 1`

str=`echo $str|tr "[a-z]" "[A-Z]"`

for i in A E I O U

do

l1=`echo $str|tr -d "$i"|wc -c`

l1=`expr $l1 - 1`

f=`expr $l - $l1`

echo The frequency of $i is $f

done

§ OUTPUT:-

[study@localhost ~]$ sh vfreq.sh

Enter the string aeiou

The frequency of A is 1

The frequency of E is 1

The frequency of I is 1

The frequency of O is 1

The frequency of U is 1

[study@localhost ~]$ sh vfreq.sh

Enter the string ram is very good boy

The frequency of A is 1

The frequency of E is 1

The frequency of I is 1

The frequency of O is 3

The frequency of U is 0

§ DISCUSSION:-

At first calculate the length of the original string using wc -c cammand and store it in the variable l.Then convert string into upper case and delete the vowel A from it then count the length of the string and store it in l1.The result of subtraction l1 from l is the frequency of the vowel A.The process will be applied again on the rest of the vowels and get the frequency of each vowels.The program can also be done using if-then condition.

§ AIM:-

Write a shell script which takes input from a text file and find the number of words which contains vowels.

§ PROGRAM CODE:-

echo The content of the file $1 is :-

echo "------------------------------------"

cat $1

echo "------------------------------------"

count=0

for i in `cat $1`

do

word=`echo $i|tr "[a-z]" "[A-Z]"`

echo $word|grep "[AEIOU]"

if [ $? -eq 0 ]

then

count=`expr $count + 1`

fi

done

echo The no. of the words cotaining vowel are $count

§ OUTPUT:-

[study@localhost ~]$ sh vwords.sh linux

The content of the file linux is :-

------------------------------------

Thh lnx is an operating system

Mndrk hs great Graphics

Lnx hv thh high security

------------------------------------

IS

AN

OPERATING

SYSTEM

GREAT

GRAPHICS

HIGH

SECURITY

The no. of the words cotaining vowel are 8

§ DISCUSSION:-

File name is provided by command line argument.tr command convert each word into upper case then grep command checks each word whether it contains vowel or not.If grep command contains the word with vowel,then the environment variable ($?) is equal to zero i.e. successful and increases the variable count by one.Finally count will contain the no. of words containing vowels.


§ AIM:-

This shell script program is to convert a large name to it's shorted representation.For example

the name is Ram Gopal Krishna Chanda Pal.The shorted representation is R.G.K.C.Pal.

§ PROGRAM CODE:-

read -p "Enter the name " name

sname=""

wcount=`echo $name|wc -w`

c=1

for i in `echo $name`

do

if [ $c -eq $wcount ]

then

sname=$sname$i

else

ch=`echo $i|cut -c 1`

sname=$sname$ch.""

fi

c=`expr $c + 1`

done

echo $sname

§ OUTPUT:-

[study@localhost ~]$ sh shrtname.sh

Enter the name Ram Gopal Krishna Chanda PAL

R.G.K.C.PAL

§ DISCUSSION:-

In the for loop i takes each word and cut the frist letter using cut command.Then update the variable sname by concatinating each letter and the last word is directly concatinated with sname.Finally sname stores the output.


§ AIM:-

Write a shell script to execute the following program-

The marks obtained by student in three different subjects should be taken as input through the keyboard.

The student gets a division as per the following rule:

Percentage above or equal to 60 --> Frist division

Percentage between 45 to 59 --> Second division

Percentage between 30 to 44 --> Third division

Percentage less than 30 --> Fail

§ PROGRAM CODE :-

Program code of Data Entry Script:-

#Data entry script

touch student.dat

echo -e "Enter the roll: \c"

read roll

if [ -z "$roll" ]

then

echo "not null constraint violated"

exit

fi

grep "^$roll:" student.dat > /dev/null

if [ $? -eq 0 ]

then

echo "unique constraint violated"

exit

fi

echo -e "Enter the name: \c"

read name

echo -e "Enter the marks of physics: \c"

read phy

echo -e "Enter the marks of math: \c"

read math

echo -e "Enter the marks of comp. sc.: \c"

read comp

echo "$roll:$name:$phy:$math:$comp" >> student.dat

§ Program code Of Report Script:-

#Report script

#This script will show the details of each student

terminal=`tty`

IFS=":"

exec <>

while read roll name phy math comp

do

echo "Roll : $roll"

echo "Name : $name"

echo "Phy : $phy"

echo "Math : $math"

echo "Comp. sc. : $comp"

total=`expr $phy + $math + $comp`

echo "--------------------------------------"

echo "Total : $total"

echo "--------------------------------------"

r=`echo "scale=2;$total / 3"|bc`

echo "percentage : $r"

flag=`echo "if ( $r >= 60 ) 1 else 0" |bc`

if [ $flag -eq 1 ]

then

echo "Frist division"

else

flag=`echo "if ( $r >= 45 ) 1 else 0" |bc`

if [ $flag -eq 1 ]

then

echo "Second division"

else

flag=`echo "if ( $r >= 30 ) 1 else 0" |bc`

if [ $flag -eq 1 ]

then

echo "Third division"

else

echo Fail

fi

fi

fi

echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

done

exec < $terminal

§ OUTPUT :-

Output Of Data Entry Script:-

[study@localhost ~]$ sh entry.sh

Enter the roll: 01

Enter the name: Souvik Das

Enter the marks of physics: 70

Enter the marks of math: 85

Enter the marks of comp. sc.: 90

[study@localhost ~]$ sh entry.sh

Enter the roll: 02

Enter the name: Sumon Pal

Enter the marks of physics: 65

Enter the marks of math: 90

Enter the marks of comp. sc.: 85

[study@localhost ~]$ sh entry.sh

Enter the roll: 01

unique constraint violated

[study@localhost ~]$ sh entry.sh

Enter the roll:

not null constraint violated

[study@localhost ~]$ sh entry.sh

Enter the roll: 03

Enter the name: Rakesh Roy

Enter the marks of physics: 45

Enter the marks of math: 55

Enter the marks of comp. sc.: 49

[study@localhost ~]$ sh entry.sh

Enter the roll: 04

Enter the name: Rohan Choudhury

Enter the marks of physics: 30

Enter the marks of math: 35

Enter the marks of comp. sc.: 33

[study@localhost ~]$ sh entry.sh

Enter the roll: 05

Enter the name: Sukesh Pal

Enter the marks of physics: 25

Enter the marks of math: 20

Enter the marks of comp. sc.: 22

Output Of Report script:-

[study@localhost ~]$ sh report.sh

Roll : 01

Name : Souvik Das

Phy : 70

Math : 85

Comp. sc. : 90

--------------------------------------

Total : 245

--------------------------------------

percentage : 81.66

Frist division

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Roll : 02

Name : Sumon Pal

Phy : 65

Math : 90

Comp. sc. : 85

--------------------------------------

Total : 240

--------------------------------------

percentage : 80.00

Frist division

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Roll : 03

Name : Rakesh Roy

Phy : 45

Math : 55

Comp. sc. : 49

--------------------------------------

Total : 149

--------------------------------------

percentage : 49.66

Second division

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Roll : 04

Name : Rohan Choudhury

Phy : 30

Math : 35

Comp. sc. : 33

--------------------------------------

Total : 98

--------------------------------------

percentage : 32.66

Third division

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Roll : 05

Name : Sukesh Pal

Phy : 25

Math : 20

Comp. sc. : 22

--------------------------------------

Total : 67

--------------------------------------

percentage : 22.33

Fail

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

§ DISCUSSION:-

The user provides the student details by data entry script and store it in student.dat file.The report script reads the data from student.dat file and calculate the total marks,percentage and division of each student.