Showing posts with label Lab Program. Show all posts
Showing posts with label Lab Program. Show all posts

Saturday, January 11, 2014

PROGRAM FOR  USAGE OF DATE AND TIME

n=`date +%H`
i=`date +%T`
echo $i
if test $n -lt 12
then
echo good morning
elif test $n -gt 12 -a $n -lt 16
then
echo good afternoon
elif test $n -gt 16 -a $n -lt 18
then
echo good evening
else
echo good night
fi
~
~
~
~
~
~
~
~
~
~
~
~
~
OUTPUT
"wishdate" 15L, 214C written
[staff@linuxserver staff]$ sh wishdate
10:03:19
good morning
[staff@linuxserver staff]$
























PROGRAM TO DETERMINE WHETHER THE GIVEN YEAR IS LEAP YEAR

echo enter a year
read n
if test `expr $n % 4` -eq 0
then
echo leap year
else
echo not a leap year
fi

~
~
OUTPUT

[staff@linuxserver staff]$ sh leap
enter a year
2008
leap year
[staff@linuxserver staff]$ sh leap
enter a year
2009
not a leap year



 PROGRAM TO GENERATE THE FINOCCI SERIES

echo "enter the number"
read n
echo "The result is"
c=0
a=0
b=1
echo "$a"
echo "$b"
n=`expr $n - 2`
while test $n -ne 0
do
c=`expr $a + $b`
echo "$c"
a=$b
b=$c
n=`expr $n - 1`
done
~



OUTPUT:


[eceb33@linuxserver eceb33]$ sh fiboo
enter the number
8
The result is
0
1
1
2
3
5
8
13


PROGRAM TO DETERMINE THE SQUARE OF A SET OF NUMBERS

echo "Enter the starting number"
read c
echo "Enter the limit number"
read b
s=0
while test $c -le $b
do
s=`expr $c \* $c`
echo "$c * $c    : $s"
c=`expr $c + 1`
done

OUTPUT:

"square" 12L, 168C written
[eceb33@linuxserver eceb33]$ sh square
Enter the starting number
5
Enter the limit number
10
5 * 5    : 25
6 * 6    : 36
7 * 7    : 49
8 * 8    : 64
9 * 9    : 81
10 * 10    : 100



PROGRAM TO DETERMINE WHETHER THE GIVEN NUMBER IS EVEN OR ODD

echo "Enter the number"
read n
if test `expr $n \% 2` -eq 0
then
echo "$n is even"
else
echo "$n is odd"


OUTPUT


[eceb33@linuxserver eceb33]$ sh odd
Enter the number
34
34 is even
[eceb33@linuxserver eceb33]$ sh odd
Enter the number
73
73 is odd







PROGRAM TO DETERMINE THE AREA OF A TRIANGLE

echo "Enter the base and height of the triangle"
read b
read h
c=`expr $b \*  $h \* $h`
area=`expr $c / 2`
echo " Area of the triangle is  $area"

OUTPUT

[staff@linuxserver staff]$ sh triangle.c
Enter the base and height of the triangle
10
8

 Area of the triangle is  320

PROGRAM TO FIND THE REVERSE OF A NUMBER

echo "enter the number"
read n
u=0
while test $n  -ne 0
do
u=`expr $n % 10`
n=`expr $n / 10`
echo $u
done



OUTPUT:
 [eceb33@linuxserver eceb33]$ sh reverse
enter the number
347
7
4
3
PROGRAM TO DETERMINE THE GIVEN NUMBER IS ARMSTRONG OR NOT

echo "Enter the no"
read n
i=0
k=$n
while test $n -ne 0
do
u=`expr $n % 10`
i=`expr $i + $u \* $u \* $u`
n=`expr $n \/ 10`
done
if test $k -eq $i
then
echo "$k is a armstrong number"
else
echo "$k is not armstrong number"
fi
~
~
"arm" 16L, 225C                                               15,1          All

Output:

"arm" 16L, 225C written
[eceb33@linuxserver eceb33]$ sh arm
Enter the no
153

153 is a armstrong number
PROGRAM TO DISPLAY EMPLOYEE DETAILS (FILE MANAGEMENT)

#include<stdio.h>
main()
{
int empno;
char empname[25],dept[25];
FILE *ptr;
ptr=fopen("emp1.dat","w");
printf("Enter Name, No, Dept\n");
scanf("%s%d%s",empname,&empno,dept);
fclose(ptr);
ptr=fopen("emp1.dat","r");
fscanf(ptr,"%s%d%s",empname,&empno,dept);
printf(" Emp Name  : %s\n",empname);
printf(" Emp No    : %d\n",empno);
printf(" Department: %s\n",dept);
}
~
"filemgt.c" 16L, 364C                                         16,1          All

OUTPUT

"filemgt.c" 16L, 364C written
[staff@linuxserver staff]$ ./a.out
Enter Name, No, Dept
rasi
68
cse
 Emp Name  : rasi
 Emp No    : 68

 Department: cse
PROGRAM TO SWAP TWO NUMBERS USING POINTERS

#include<stdio.h>
void swap(int *,int *);
main()
{
//swap(int,int);
int a=10,b=20;
printf("Before Swapping:\na=%d\nb=%d\n",a,b);
swap(&a,&b);
}
void swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf("After Swapping:\na=%d\nb=%d\n",*x,*y);
}
~
~
"swapp.c" 17L, 246C                                           16,1          All~
~

OUTPUT:

"swapp.c" 17L, 232C written
[staff@linuxserver staff]$ cc swapp.c
[staff@linuxserver staff]$ ./a.out
Before Swapping:
a=10
b=20
After Swapping:
a=20

b=10
PROGRAM TO DISPLAY STUDENT DETAILS USING POINTERS

#include<stdio.h>
main()
{
int regno,m1,m2,m3,total;
float avg;
char name[25];
int *p1,*p2,*p3;
p1=&m1;
p2=&m2;
p3=&m3;
printf("Enter sutdent name: \n");
scanf("%s",name);
puts("Enter regno: ");
scanf("%d",&regno);
printf("Enter the 3 subject marks: \n");
scanf("%d\n%d\n%d",&m1,&m2,&m3);
total=*p1+ *p2+ *p3;
avg=(float)total/3;
printf("Student Details:\n");
printf("Name         : %s\n",name);
printf("RegNo        : %d\n", regno);
printf("Subject mark1: %d\n",*p1);
printf("Subject mark2: %d\n",*p2);
printf("Subject mark3: %d\n",*p3);
printf("Total        : %d\n",total);
printf("Average      : %f\n",avg);
}

-- INSERT --                                                  19,25         All~

OUTPUT:

"stud_pter.c" 28L, 613C written
[staff@linuxserver staff]$ cc stud_pter.c
[staff@linuxserver staff]$ ./a.out
Enter sutdent name:
rasi
Enter regno:
4240668
Enter the 3 subject marks:
78
67
90

Student Details:
Name         : rasi
RegNo        : 4240668
Subject mark1: 78
Subject mark2: 67
Subject mark3: 90
Total        : 235
Average      : 78.333336



PROGRAM TO PERFORM MENU DRIVEN ARITHMETIC OPERATIONS USING FUNCTION


#include<stdio.h>
main()
{
int ch,a,b,c;
printf("Perform arithmetic functions\n");
printf("Enter a,b\n");
scanf("%d\n%d",&a,&b);
while(1)
{
printf("\n1.ADDITION\n2.subtraction\n3.multiplication\n4.division\n");
printf("Enter UR Choice:  ");
scanf("\n%d",&ch);
switch(ch)
{
case 1:
add(a,b);
break;

case 2:
sub(a,b);
break;

case 3:
mul(a,b);
break;

case 4:
div(a,b);
break;

default:
printf("wrong value\n");
exit(0);
}
}
}

int add(int p,int q)
{
int c;
c=p+q;
printf("Answer is: %d\n",c);
}

int sub(int p,int q)
{
int c;
c=p-q;
printf("Answer is: %d\n",c);
}




int mul(int p,int q)
{
int c;
c=p*q;
printf("Answer is: %d\n",c);
}

int div(int p,int q)
{
int c;
c=p/q;
printf("Answer is: %d\n",c);
}

OUTPUT:
"menudriven.c" 70L, 777C written
[staff@linuxserver staff]$ cc menudriven.c
[staff@linuxserver staff]$ ./a.out
Perform arithmetic functions
Enter a,b
12
3
1.ADDITION
2.subtraction
3.multiplication
4.division
Enter UR Choice:  2
Answer is: 9

1.ADDITION
2.subtraction
3.multiplication
4.division
Enter UR Choice:  1
Answer is: 15

1.ADDITION
2.subtraction
3.multiplication
4.division
Enter UR Choice:  3
Answer is: 36

1.ADDITION
2.subtraction
3.multiplication
4.division
Enter UR Choice:  4
Answer is: 4

1.ADDITION
2.subtraction
3.multiplication
4.division
Enter UR Choice:  5

wrong value
PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER USING RECURSION

#include<stdio.h>
main()
{
int f,n;
printf("Enter the value for finding factorial\n");
scanf("%d",&n);
f=fact(n);
printf("Factorial of %d is %d\n",n,f);
}
int fact(int a)
{
int fa;
if(a==1||a==0)
return 1;
else
fa=a*fact(a-1);
return(fa);
}


~

~
"factrec.c" 20L, 243C                                         13,1          All


OUTPUT:

"factrec.c" 20L, 243C written
[staff@linuxserver staff]$ ./a.out
Enter the value for finding factorial
7
Factorial of 7 is 5040
[staff@linuxserver staff]$










 PROGRAM TO STORE A STRING OF 5 CHARACTERS (DYNAMIC ALLOCATION)

#include<stdio.h>
#include<malloc.h>
main()
{
char *cp;
int i;
cp=(char *)calloc(5,1);
printf("the storage address of memory\n");
printf("Block address :%u\n",cp);
printf("enter the string having 5 characters\n");
for(i=1;i<=5;i++)
{
*cp++=getchar();
printf("the character %d is %c\n",i,*(cp-1));
printf("it is stored in the address  %u\n",(cp-1));
}
}


"dynamic.c" 18L, 354C                                         10,1          All


OUTPUT:

"dynamic.c" 18L, 354C written
[staff@linuxserver staff]$ cc dynamic.c
[staff@linuxserver staff]$ ./a.out
the storage address of memory
Block address :134518568
enter the string having 5 characters
nisha
the character 1 is n
it is stored in the address  134518568
the character 2 is i
it is stored in the address  134518569
the character 3 is s
it is stored in the address  134518570
the character 4 is h
it is stored in the address  134518571
the character 5 is a
it is stored in the address  134518572