Friday, May 27, 2011

Shell Programs


Program for Fibonacci Series

echo To find the Fibonacci Series
echo Enter a number
read b
c=0
e=1
echo $c
echo $e
while test $e -le $b
do
echo $d
d=`expr $c + $e`
c=$e
e=$d
done



OUTPUT
To find the Fibonacci series
enter a numbers :3
0
1
1
2
3








Program for Armstrong Number Between 1 to 500

echo AMSTRONG NUMBERS BETWEEN 1 TO 500
for (( i=1 ; i<= 500 ; i++ ))
do
temp=$i
n=$i
ams=0
while test $n -ne 0
do
d=`expr $n % 10`
ams=`expr $ams + $d \* $d \* $d`
n=`expr $n / 10`
done
if test $temp -eq $ams
then
echo $ams
fi
done



OUTPUT:
ARMSTRONG NUMBER BETWEEN 1 TO 500
1
153
370
371
407











PROGRAM FOR REVERSE OF NUMBER:

echo enter the number to be reversed
read n
rev=0
while test $n -ne 0
do
a=`expr $n % 10`
rev=`expr $rev \* 10 + $a`
n=`expr $n / 10`
done
echo REVERSE OF A NUMBER =$rev
OUTPUT:
enter the number to be reversed
25463
REVERSE OF A NUMBER=36452
Program for Greatest of 3 Number
echo greatest of 3 numbers
echo enter the numbers
read a
read b
read c
if test $a -gt $b -a $a -gt $c
then
echo a is greater
elif test $b -gt $c
then
echo b is greater
else
echo c is greater
fi
done



OUTPUT
great of 3 numbers
enter the numbers
25
65
80
c is greater







Program for Prime Number Between 1 to 50

echo PRIME NOS BETWEEN 1 TO 50
for (( i=2 ; i<=50 ; i++ ))
do
if test $i -eq 2 -o $i -eq 3 -o $i -eq 5 -o $i -eq 7
then
echo $i
elif test `expr $i % 2` -ne 0 -a `expr $i % 3` -ne 0 -a `expr $i % 7` -ne 0
then
echo $i
fi
done



OUTPUT:
PRIME NUMBERS BETWEEN 1 TO 50
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47








Program for String Palindrome

echo Reversing the number
echo Enter the string
read a
b=`expr $a | wc -c`
c=`expr $b - 1`
echo $c
while test $b -gt 0
do
e=`expr $a | cut -c $b`
d=`echo $d$e`
b=`expr $b - 1`
done
echo The reversed string is $d
if test $a = $d
then
echo It is a palindrome
else
echo It is not a palindrome
fi
done



OUTPUT
enter a string
malayalam
10
the reverse sting is malayalam
It is a palindrome









Program for Employee Details

echo Enter the number of employees
read n
for (( i=1 ; i<=$n ; i++ ))
do
echo Enter the employee ID Number
read empno
echo Enter the employee name
read ename
echo Enter the employees basic salary
read bsal
if test $bsal -ge 10000
then
hra=1000
da=700
ta=500
elif test $bsal -lt 10000 -a $bsal -ge 5000
then
hra=750
da=500
ta=350
elif test $bsal -lt 5000 -a $bsal -ge 3000
then
hra=500
da=300
ta=200
else
hra=300
da=200
ta=100
fi
netsal=`expr $hra + $da + $ta + $bsal`
echo --------------------------------------------------------
echo PAY SLIP OF $ename
echo --------------------------------------------------------
echo Empname $ename
echo Empno $empno
echo Basicpay=$bsal
echo HRA=$hra
echo TA=$ta
echo DA=$da
echo Netpay=$netsal
done



OUTPUT:
enter the number of employees
2
enter the employee id number 4777
enter the employee name kumaran
enter the employee basic salary25000
empname=kumaran
empno=4777
basicpay=25000
HRA=1000
DA=700
TA=500
netpay=27200
enter the employee id number 4778
enter the employee name srini
enter the employee basic salary 4000
empname=srini
empno=4778
basicpay=4000
HRA=500
TA=200
DA=300
netpay=5000








Program for Performing Arithmetic Operators

echo Program For Arithmetic Operator
echo Enter The First Number
read a
echo Enter The Second Number
read b
add=`expr $a + $b`
sub=`expr $a - $b`
mul=`expr $a \* $b`
div=`expr $a / $b`
echo OUTPUT
echo "The Sum is:"$add
echo "The Difference is:"$sub
echo "The Product is:"$mul
echo "The Quotient is:"$div



OUTPUT:
Program for Arthmetic Operator
Enter the First number
15
Enter the Second number
5
The Sum is :20
The Difference is :10
The Product is :75
The Quotient is :3






Program for Student Details

echo enter the number of students
read n
for (( i=1 ; i<=$n ; i++ ))
do
echo Enter the Register Number
read reg
echo Enter the student name
read sname
echo Enter the 5 subjects marks
read m1
read m2
read m3
read m4
read m5
tot=`expr $m1 + $m2 + $m3 + $m4 + $m5`
avg=`expr $tot / 5`
echo =============================================
echo $sname STUDENT DETAILS
echo ==============================================
echo STUDENT NAME=$sname
echo STUDENT REGNO=$reg
echo STUDENT AVERAGE=$tot
if test $avg -ge 75
then
echo GRADE="S" $name passed with DISTINCTION
elif test $avg -lt 75 -a $avg -ge 60
then
echo GRADE="A" $name passed with FIRST CLASS
elif test $avg -lt 60 -a $avg -ge 50
then
echo GRADE="B" $name passed with SECOND CLASS
elif test $avg -lt 50 -a $avg -ge 45
then
echo GRADE="C" $name passed with THIRD CLASS
else
echo GRADE="U" $name FAILED
fi
done



OUTPUT:
enter the number of students
1
Enter the Register Number
453467
Enter the student name
Kumaran
Enter the 5 subjects marks
90
98
89
90
100
=============================================
Kumaran STUDENT DETAILS
==============================================
STUDENT NAME=Kumaran
STUDENT REGNO=453467
STUDENT AVERAGE=467
GRADE=S passed with DISTINCTION









Program for Multiplication Table

echo Program for Multiplication Table
i=1
echo Enter the value of the table
read n
echo output
while test $i -le $n
do
echo The multiplication table for $i
for j in 1 2 3 4 5 6 7 8 9 10
do
a=`expr $i \* $j`
echo $i x $j = $a
done
i=`expr $i + 1`
done



OUTPUT
Program for Multiplication Table
Enter the value of the table
2
The multiplication table for:1
1x1=1
1x2=2
1x3=3
1x4=4
1x5=5
1x6=6
1x7=7
1x8=8
1x9=9
1 x 10 = 10
The multiplication table for:2
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

C programs


PROGRAM FOR CALL BY VALUE
#include<stdio.h>
int main()
{
void swap (int a,int b);
int a,b;
printf("Enter the two numbers to Swap:");
scanf("%d %d",&a,&b);
swap(a,b);
return(0);
}
void swap (int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("After Swapping : %d %d\n",a,b);
}



OUTPUT:
Enter the two numbers to Swap:
15 87
After Swapping : 87 15







PROGRAM FOR FACTORIAL FINDING NCR:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int fact(int k);
int n,r,ncr;
printf("\n Enter value to n and r:");
scanf("%d %d", &n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("\n Value of ncr = %d \n",ncr);
}
int fact(int k)
{
int i, p=1;
for(i=1;i<=k;i++)
p=p*i;
return(p);
}


OUTPUT:
Enter value to n and r: 4 2
Value of ncr = 6










PROGRAM TO FIND ADDRESS OF VARIABLE
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a=22;
int *b;
int **c;
b=&a;
c=&b;
printf("\n value of a is %d",a);
printf("\n value of a is %d",*(&a));
printf("\n value of a is %d",*b);
printf("\n value of a is %d",**c);
printf("\n value of b and address of a=%u",b);
printf("\n value of c and address of b=%u",c);
printf("\n address of a=%u",&a);
printf("\n address of b=%u",&b);
printf("\n address of a=%u",*c);
printf("\n address of b=%u",&b);
printf("\n address of b=%u",c);
printf("\n address of c=%u",&c);
return(0);
}



OUTPUT:
value of a is 22
value of a is 22
value of a is 22
value of a is 22
value of b and address of a=3215158316
value of c and address of b=3215158312
address of a=3215158316
address of b=3215158312
address of a=3215158316
address of b=3215158312
address of b=3215158312
address of c=3215158308









PROGRAM FOR SWAPPING OF NUMBER USING CALL BY REFERENCE:
#include<stdio.h>
int main()
{
void swap (int*a,int*b);
int a,b;
printf("Enter a,b");
scanf("%d %d",&a,&b);
swap(&a,&b);
printf("After swapping %d %d\n",a,b);
return(0);
}
void swap (int*a,int*b)
{
int t;
t=*a;
*a=*b;
*b=t;
}



OUTPUT:
Enter a,b 30 11
After swapping 11 30











PROGRAM TO PRINT THE STRING IN REVERSE ORDER USING POINTER:
#include<stdio.h>
#include<string.h>
int main()
{
char a[50];
void reverse(char *);
printf("\n Enter the String: ");
scanf("%s",a);
reverse(a);
printf(" \n After reversing the String is : \n");
puts(a);
}
void reverse(char *string)
{
char *lp = string; /*left pointer*/
char *rp = &string[strlen(string)-1]; /*right pointer*/
char tmp;
while(lp < rp)
{
tmp = *lp;
*lp = *rp;
*rp = tmp;
lp++;
rp--;
}
}



OUTPUT:
Enter the String: kumaran
After reversing the String is :
naramuk












PROGRAM TO ALTER ALLOCATED MEMORY:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
char *p;
p=(char *)malloc(6);
strcpy(p,"MADRAS");
printf("memory contains:%s\n",p);
p=(char *)realloc(p,7);
strcpy(p,"CHENNAI");
printf("memory now contains:%s\n",p);
free(p);
}



OUTPUT:
memory contains:MADRAS
memory now contains:CHENNAI













PROGRAM TO PRINT VARIABLE FROM MEMORY ADDRESS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int *a, *n,size;
printf("enter the size...");
scanf("%d", &size);
n =(int*)malloc(size*sizeof(int));
printf("Address of the first byte is ....%u\n", n);
printf("Enter the values....");
for(a=n;a<n+size; a++)
scanf("%d", a);
printf("Printing the values....\n");
for(a=n+size-1;a>=n;a--)
printf("%d is stored in address %u \n", *a,a);
return(0);
}



OUTPUT:
enter the size...2
Address of the first byte is ....144461832
Enter the values....54
21
Printing the values....
21 is stored in address 144461836
54 is stored in address 144461832

















PROGRAM TO MULTIPLY TWO MATRICES:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a[20], *b[20], *c[20];
int i, j,r1, c1, r2, c2;
void get();
void display();
void mul();
printf(" Enter size of matrix A:\n");
scanf("%d %d", &r1, &c1);
printf(" Enter size of matrix B:\n");
scanf("%d %d", &r2, &c2);
if(c1 != r2)
{
printf("\n Multiplication not Possible \n");
printf("Column of matrix A are not equal to Rows of matrix B \n");
}
else
{
printf("\n Multiplication Possible \n");
for(i=0,j=0;i<r1,j<r2;i++,j++)
{
a[i]=(int*)malloc(c1*sizeof(int));
b[j]=(int*)malloc(c2*sizeof(int));
c[i]=(int*)malloc(c2*sizeof(int));
}
printf("Enter matrix A(%d X %d) elements \n", r1, c1);
get(a,r1,c1);
display(a,r1,c1);
printf("\n");
printf("Enter matrix B(%d X %d) elements \n", r2, c2);
get(b,r2,c2);
display(b,r2,c2);
printf("\n");
mul(a,b,c,r1,c1,c2);
printf(" The Product of matrix C(%d X %d) \n", r1, c2);
display(c,r1,c2);
}
return (0);
}
void get(x,r,c)
int *x[10],r,c;
{
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",x[i]+j);
}
void display(x,r,c)
int *x[10],r,c;
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%5d",*(x[i]+j));
printf("\n");
}
}
void mul(a,b,c,row,m,col)
int *a[10],*b[10],*c[10],row,m,col;
{
int i,j,k;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
*(c[i]+j)=0;
for(k=0;k<m;k++)
*(c[i]+j)=*(c[i]+j)+(*(a[i]+k))*(*(b[k]+j));
}
}
}



OUTPUT:
Enter size of matrix A:
3
3
Enter size of matrix B:
3
3
Multiplication Possible
Enter matrix A(3 X 3) elements
9
8
7
6
5
4
3
2
1
987
654
321
Enter matrix B(3 X 3) elements
1
2
3
4
5
6
7
8
9
123
456
789
The Product of matrix C(3 X 3)
90 114 138
54 69 84
18 24 30












PROGRAM TO CREATE A FILE AND READ THE CONTENT:
#include<stdio.h>
int main()
{
char empname[25];
int empno;
char deptname[25];
FILE *fptr;
fptr = fopen("employee.txt","w");
printf("Enter the Employee Name, Number and hisDepartment Name \n");
scanf("%s %d %s",empname,&empno,deptname);
fprintf(fptr,"%s%d%s", empname,empno,deptname);
fptr=fopen("employee.txt","r");
fscanf(fptr,"%s %d %s",empname,&empno, deptname);
printf("\nEmployee Details");
printf("\n---------------------");
printf("\n Employee Name : %s",empname);
printf("\n Employee Number : %d",empno);
printf("\n Department Name : %s \n \n", deptname);
printf(“--------------------------------------------”);
return(0);
}



OUTPUT:
Enter the Employee Name, Number and hisDepartment Name
kumaran 4777 cse
Employee Details
-------------------------------------
Employee Name : kumaran
Employee Number : 4777
Department Name : cse
--------------------------------------















PROGRAM TO COPY FILE CONTENT FROM ONE FILE TO ANOTHER:
#include<stdio.h>
int main()
{
FILE *fp1, *fp2;
int ch;
char fname1[30], fname2[30];
printf("Enter Source File:");
scanf("%s", fname1);
printf("Enter Destination File:");
scanf("%s", fname2);
fp1 = fopen(fname1,"r");
fp2 = fopen(fname2,"w");
if(fp1==NULL)
{
printf("Cannot Open the file %s for reading \n", fname1);
}
else if(fp2==NULL)
{
printf("Cannot Open the file %s for writing \n",
fname2);
}
else
{
ch=getc(fp1);
while(ch != EOF)
{
putc(ch, fp2);
ch = getc(fp1);
}
fclose(fp1);
fclose(fp2);
printf("Files Sucessfully Copied \n");
}
return(0);
}



OUTPUT:
Enter Source File:ref.c
Enter Destination File:cse a
Files Sucessfully Copied










PROGRAM TO DELETE A FILE:
#include<stdio.h>
int main()
{
char file[80];
printf("File to Delete : ");
scanf("%s",file);
if(remove(file) == 0)
printf("Removed %s. \n", file);
else
printf("Not able to remove, File not Found \n");
return 0;
}



OUTPUT:
File to Delete : cse a
Removed cse.











PROGRAM TO RENAME A FILE:
#include<stdio.h>
int main()
{
char oldname[80], newname[80];
printf("File to Rename: ");
scanf("%c",oldname);
printf("New Name : ");
scanf("%c",newname);
if(rename(oldname,newname) == 0)
printf("Renamed %s to %s.\n",
oldname,newname);
else
printf("Not able to rename, File not Found \n");
return 0;
}



OUTPUT:
File to Rename:cse a
New name:cse b
Renamed cse a to cse b