Vous êtes sur la page 1sur 170

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 1

CS010 307(P): Programming Lab

EXP NO: 1
DATE: 08/07/2014

AREA OF SQUARE, RECTANGLE AND CIRCLE


AIM
To write a menu driven program for finding the area of a square , rectangle , and a circle
ALGORITHM
Step 1: Start
Step 2: Print choice square , rectangle , circle
Step 3: if square ,else go to step 7
Step 4: read side
Step 5: as*s
Step 6:Print a
Step 7:if rectangle,else go to step 11
Step 8: read length and breadth
Step 9: alenght*breadth
Step 10: print a
Step 11: if circle,else go to step15
Step 12: read radius
Step 13: aradius*radius*3.14
Step 14: print a
Step 15: print want to continue?
Step 16: if yes,go to step 1
Step 17: stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
char t,n,y,Y;

Department of Computer Science & Engineering

Page 2

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 3

CS010 307(P): Programming Lab

float radius,side,length,breadth,pi;
int ch;
pi=3.14;
do
{
printf("\nMENU\n1)Area of square\n2)Area of rectangle\n3)Area of circle\nEnter choice:");
scanf("%d",&ch);
switch (ch)
{
case 1:
printf("Enter the value of side\n");
scanf("%f",&side);
printf("the area of the square= %f*%f=%f cm",side,side,side*side);
break;
case 2:
printf("\nEnter the value of length\n");
scanf("%f",&length);
printf("\nEnter the value of breadth\n");
scanf("%f",&breadth);
printf("the area of the rectangle= %f*%f=%f cm",length,breadth,length*breadth);
break;
case 3:
printf("\nEnter the value of radius");
scanf("%f",&radius);
printf("the area of the circle= %f*%f*pi=%f cm",radius,radius,radius*radius*pi);
break;
default:printf("INVALID CHOICE");
break;
}
printf("\n\nDO YOU WANT TO CONTINUE:\nThen press y else n:");
fflush(stdin);
Department of Computer Science & Engineering

Page 4

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 5

CS010 307(P): Programming Lab

scanf("%c",&t);
}
while(t=='y'||t=='Y');
getch();
}
RESULT
The program for finding the area of a square , rectangle , and a circle was written , executed and
the output verified successfully.

Department of Computer Science & Engineering

Page 6

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 7

CS010 307(P): Programming Lab

EXP NO: 2
DATE: 08/07/2014

VOLUME OF CUBE AND SPHERE


AIM
To write a menu driven program for finding the volume of a sphere and a cube
ALGORITHM
Step 1: Start
Step 2: Print choice sphere ,cube
Step 3: if cube go to step 7
Step 4: read radius r
Step 5: vr*r*r*(4/3)*3.14
Step 6: Print v
Step 7: read side s
Step 8: vs*s*s
Step 9: print v
Step 10: print want to continue?
Step 11: if yes,go to step 1
Step 12: stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
char t,y,Y,n;
float radius,side,pi;
int ch;
pi=3.14;
do
{

Department of Computer Science & Engineering

Page 8

CS010 307(P): Programming Lab

printf("\nMENU\n1)volume of sphere\n2)volume of cube\n");


Department of Computer Science & Engineering

Page 9

CS010 307(P): Programming Lab

scanf("%d",&ch);
switch (ch)
{
case 1:
printf("Enter the value of radius\n");
scanf("%f",&radius);
printf("the volume of the sphere=%f cm3",radius*radius*radius*pi*(1.33));
break;
case 2:
printf("\nEnter the value of side\n");
scanf("%f",&side);
printf("the volume of the cube=%f cm3",side*side);
break;
default:printf("INVALID CHOICE");
break;
}
printf("\n\nDO YOU WANT TO CONTINUE:\nThen press y else n:");
fflush(stdin);
scanf("%c",&t);
}
while(t=='y'||t=='Y');
getch();
}
RESULT
The program for finding the volume of sphere and cube was written , executed and the output
verified successfully.

Department of Computer Science & Engineering

Page 10

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 11

CS010 307(P): Programming Lab

EXP NO: 3
DATE: 22/07/2014

SWAPPING TWO NUMBERS WITHOUT USING TEMPORARY


VARIABLE
AIM
To write a C program for swapping two numbers without using temporary variable
ALGORITHM
Step 1: Start
Step 2: Read two numbers a,b
Step 3: aa+b
Step 4: ba-b
Step 5: aa-b
Step 6:Print the numbers a,b
Step 7:Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("Before swapping\na=%d\nb=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swapping\na=%\nb =%d ",a,b);
getch();
}
RESULT
The C program for swapping two numbers is completed successfully and the output is verified.
Department of Computer Science & Engineering

Page 12

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 13

CS010 307(P): Programming Lab

EXP NO: 4
DATE: 22/07/2014

TEMPERATURE CONVERSION FROM CELSIUS SCALE TO


FAHRENHEIT AND VICE VERSA
AIM
To write a C program for temperature conversion from celsius scale to fahrenheit and vice versa
ALGORITHM
Step 1: Start
Step 2: Read celsius temperature
Step 3: Multiply with (9/5) and add 32
Step 4: Print value in fahrenheit
Step 5: Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int c,f;
printf("Enter the celsius scale \n");
scanf("%d",&c);
f=((9*c)/5)+32;
printf("The fahrenheit scale is %d",f);
getch();
}

RESULT
The C program for

temperature conversion from celsius to farhenheit and vice versa is

completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 14

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 15

CS010 307(P): Programming Lab

EXP NO: 5
DATE: 22/07/2014

ROOTS OF A QUADRATIC EQUATION


AIM
To write a C program for finding roots of a quadratic equation
ALGORITHM
Step 1: Start
Step 2: Read values of a,b and c
Step 3: Calculate value of discriminant
Db*b-(4*a*c)
Step 4: if D0
Root(-1*b/(2*a))
Display Root
Step 5: if D>0
Root1(-b+D)/2a
Root2(-b-D)/2a
Display Root1 and Root2
Step 6: if D<0, Display roots are imaginary
Step 7: Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2;
printf("Enter the values of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
d=(b*b)-(4*a*c);
r1=((-1*b)+sqrt(d))/(2*a);
Department of Computer Science & Engineering

Page 16

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 17

CS010 307(P): Programming Lab

r2=(-b-sqrt(d))/(2*a);
printf("Roots are real and distinct:\n%f\n%f",r1,r2);
}
else if(d==0)
{
r1=-b/(2*a);
printf("Roots are real and equal:\n%f",r1);
}
else
{
printf("The roots are imaginary");
}
getch();
}

RESULT
The C program for findng the roots of a quadratic equation is completed successfully and the
output is verified.

Department of Computer Science & Engineering

Page 18

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 19

CS010 307(P): Programming Lab

EXP NO: 6
DATE: 22/07/2014
ODD OR EVEN NUMBER
AIM
To write a C program to check the given number is odd or even.
ALGORITHM
Step 1: Start
Step 2: Read a
Step 3: if (a%2=0), if not go to step 5
Step 4: Print Given number is even
Step 5: Print Given number is odd
Step 6: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(Enter a number:);
scanf(%d,&a);
if (a%2==0)
printf(\nGiven number is even.);
else
printf(\nGiven number is odd.);
getch();
}

RESULT
The C program to check the given number is odd or even is completed successfully and the
output is verified.

Department of Computer Science & Engineering

Page 20

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 21

CS010 307(P): Programming Lab

EXP NO: 7
DATE: 01/08/2014
AVERAGE MARK AND GRADE OF STUDENTS USING SWITCH
AIM
To write a C program to calculate the average mark and grade of students using switch.
ALGORITHM
Step 1: Start
Step 2: Read n, m1, m2, m3, m4, m5, i0
Step 3: if (i<n), if not go to step 19
Step 4: avg(m1+m2+m3+m4+m5)/5
Step 5: i(i+1), avg1(avg*100)
Step 6: if (avg1>90), if not go to step 8
Step 7: Print Grade A, avg, go to step 18
Step 8: if (avg1>80), if not go to step 10
Step 9: Print Grade B, avg, go to step 18
Step 10: if (avg1>70), if not go to step 12
Step 11: Print Grade C, avg, go to step 18
Step 12: if (avg1>60), if not go to step 14
Step 13: Print Grade D, avg, go to step 18
Step 14: if (avg1>50), if not go to step 16
Step 15: Print Grade E, avg, go to step 18
Step 16: if (avg1<=50)
Step 17: Print Grade F, avg
Step 18: Go to step 3
Step 19: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
Department of Computer Science & Engineering

Page 22

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 23

CS010 307(P): Programming Lab

{
float m1,m2,m3,m4,m5,avg;
int grade;
printf("Enter mark of 5 subjects\n");
scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
avg=(m1+m2+m3+m4+m5)/3;
printf("Average = %f\n",avg);
if(avg>=85)
grade=1;
else if(avg>=70)
grade=2;
else if(avg>=60)
grade=3;
else if(avg>=50)
grade=4;
else if(avg>=40)
grade=5;
else
grade=6;
switch(grade)
{
case 1:
printf("grade is A");
break;
case 2:
printf("grade is B");
break;
case 3:
printf("grade is C");
break;
case 4:
Department of Computer Science & Engineering

Page 24

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 25

CS010 307(P): Programming Lab

printf("grade is D");
case 5:
printf("grade is E");
break;
case 6:
printf("FAIL");
break;
}
getch();
}

RESULT
The C program to calculate the average mark and grade of students using switch is completed
successfully and the output is verified.

Department of Computer Science & Engineering

Page 26

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 27

CS010 307(P): Programming Lab

EXP NO: 8
DATE: 01/08/2014
n NATURAL NUMBERS AND ITS SUM
AIM
To write a C program to find the sum of n natural numbers and its sum.
ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: if (n=0), if not go to step 5
Step 4: Print Invalid number, go to step 2
Step 5: in, s1, Print i
Step 6: if (i=0), if not go to step 8
Step 7: Print s, go to step 10
Step 8: s(s+1)
Step 9: i(i-1), go to step 4
Step 10: Stop
PROGRAM

#include<conio.h>
#include<stdio.h>
void main()
{
int i,n,s=0;
printf(Enter your limit:);
scanf(%d,&n);
if (n==0)
printf(\nEnter a valid number);

Department of Computer Science & Engineering

Page 28

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 29

CS010 307(P): Programming Lab

printf(Numbers:);
for (i=1;i<=n;i++)
{
printf(%d\t,i);
s=s+i;
}
printf(\nSum:%d,s);
getch();
}
RESULT
The C program to find the sum of n natural numbers and its sum is completed successfully and
the output is verified.

Department of Computer Science & Engineering

Page 30

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 31

CS010 307(P): Programming Lab

EXP NO: 9
DATE: 01/08/2014
SUM OF DIGITS OF A NUMBER
AIM
To write a C program to find the sum of digits of a given number.
ALGORITHM
Step 1: Start
Step 2: Read num
Step 3: r(num%10)
num(num/10)
sum(sum+r)
Step 4: if (num>0) go to step 3
Step 5: Print sum
Step 6: Stop
PROGRAM
#include<conio.h>
void main()
{
int num,r,s=0;
printf("Enter the number\n");
scanf("%d",&num);
while(num>0)
{
r=num%10;
s=s+r,num=num/10;
}
printf("Sum of digits = %d",s);
}
RESULT
The C program to find the sum of digits of a given number is completed successfully and the
output is verified.
Department of Computer Science & Engineering

Page 32

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 33

CS010 307(P): Programming Lab

EXP NO: 10
DATE: 01/08/2014
REVERSE OF A NUMBER
AIM
To write a C program to find the reverse of a given number.
ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: i=n
Step 4: if (n<=0), go to step 9
Step 5: b(n%10)
Step 6: r(r*10+b)
Step 7: n(n/10), go to step 4
Step 8: Print r
Step 9: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a,r=0,b=0;
printf(\nEnter the number:);
scanf(%d,&n);
a=n;
for(i=a;n>0;i--)
{
b=(n%10);
r=((r*10)+b);
n=(n/10);
}
printf(\nReverse of %d is %d,a,r);
Department of Computer Science & Engineering

Page 34

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 35

CS010 307(P): Programming Lab

getch();
}
RESULT
The C program to find the reverse of a given number is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 36

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 37

CS010 307(P): Programming Lab

EXP NO: 11
DATE: 01/08/2014
ARMSTRONG OR NOT
AIM
To write a C program to check the given number is armstrong or not.
ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: s0, in
Step 4: b(n%10)
s(s+(b*b*b)
n(n/10)
Step 5: if (i=0), if not go to step 4
Step 6: if (s=n), if not go to step 8
Step 7: Print Armstrong
Step 8: Print Not armstrong
Step 9: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,b,s=0,a;
printf(Enter a number:);
scanf(%d,&n);
a=n;
for(i=n;i!=0;i--)
{
b=n%10;
s=s+(b*b*b);
n=n/10;
Department of Computer Science & Engineering

Page 38

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 39

CS010 307(P): Programming Lab

}
if (a==s)
printf(\nThe number is armstrong);
else
printf(\nThe number is not armstrong);
getch();
}
RESULT
The C program to check the given number is armstrong or not is completed successfully and the
output is verified.

Department of Computer Science & Engineering

Page 40

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 41

CS010 307(P): Programming Lab

EXP NO: 12
DATE: 12/08/2014
FIBONACCI SERIES UPTO A LIMIT
AIM
To write a C program to print fibonacci up series to a limit.
ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: t0, s1, i3, f0
Step 4: Print f, s
Step 5: if (i>n), go to step 10
Step 6: t(f+s)
Step 7: Print t
Step 8: fs, st
Step 9: i(i+1), go to step 5
Step 10: Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,i,c=1,n;
printf("Enter the limit\n");
scanf("%d",&n);
printf("%d",a);
for(i=2;i<=n;i++)
{
printf("\t%d",c);
c=a+b,a=b,b=c;
}
Department of Computer Science & Engineering

Page 42

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 43

CS010 307(P): Programming Lab

getch();
}

RESULT
The C program to print fibonacci up series to a limit is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 44

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 45

CS010 307(P): Programming Lab

EXP NO: 13
DATE: 12/08/2014
LARGEST AMONG n NUMBERS
AIM
To write a C program to find largest among n numbers.
ALGORITHM
Step 1: Start
Step 2: Read n, num
Step 3: i1, maxnum
Step 4: if (i>n), go to step 8
Step 5: if (num>max), maxnum
Step 6: i(i+1), go to step 4
Step 7: Print max
Step 8: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,num,max;
printf("Enter limit\n");
scanf("%d",&n);
printf("Enter the numbers\n");
scanf("%d",&num);
max=num;
for(i=2;i<=n;i++)
{
scanf("%d",&num);
if(max<num)
max=num;
}
Department of Computer Science & Engineering

Page 46

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 47

CS010 307(P): Programming Lab

printf("Largest number is %d",max);


getch();
}

RESULT
The C program to find largest among n numbers is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 48

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 49

CS010 307(P): Programming Lab

EXP NO: 14
DATE: 12/08/2014
HELLO 10 TIMES USING GOTO AND IF
AIM
To write a C program to print hello 10 times using goto and if statements.
ALGORITHM
Step 1: Start
Step 2: Read n, i1
Step 3: if (i>n), go to step 6
Step 4: Print HELLO
Step 5: i(i+1), go to step 3
Step 6: Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
n:
if(i<=10)
{
printf("HELLO\n");
i++;
goto n;
}
getch();
}
RESULT
The C program to print hello 10 times using goto and if statements is completed successfully
and the output is verified.
Department of Computer Science & Engineering

Page 50

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 51

CS010 307(P): Programming Lab

EXP NO: 15
DATE: 12/08/2014
PRIME NUMBERS WITHIN A LIMIT
AIM
To write a C program to print prime numbers within a limit.
ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: i1, j1,c0
Step 4: if (j<=i), if not go to step 8
Step 5: if (i%j=0), if not go to step 4
Step 6: c(c+1)
Step 7: if (c=0), Print i
Step 8: if (i<=n), go to step 4
Step 9: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,c;
printf("Enter the limit\n");
scanf("%d",&n);
printf("Prime Numbers: \n");
for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
Department of Computer Science & Engineering

Page 52

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 53

CS010 307(P): Programming Lab

}
if(c==2)
printf("\t%d",i);

}
getch();
}
RESULT
The C program to print prime numbers within a limit is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 54

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 55

CS010 307(P): Programming Lab

EXP NO: 16
DATE: 12/08/2014
SEQUENCE 1, 2, 4, 8, 16, 32, 64
AIM
To write a C program to print sequence 1, 2, 4, 8, 16, 32, 64,.....
ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: i0, a1
Step 4: if (i<n), if not go to step 7
Step 5: Print a, i(i+1)
Step 6: aa*2, go to step 4
Step 7: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a=1;
printf(Enter your limit:);
scanf(%d,&n);
printf(\nYour Series:\n);
for(i=0;i<n;i++)
{
printf(%d\t,a);
a=a*2;
}
}
RESULT
The C program to print sequence 1, 2, 4, 8, 16, 32, 64,..... is completed successfully and the
output is verified.
Department of Computer Science & Engineering

Page 56

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 57

CS010 307(P): Programming Lab

EXP NO: 17
DATE: 12/08/2014
PATTERN
AIM
To write a C program to print the given pattern.
*
*

ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: i1, j1
Step 4: if (i>n), go to step 10
Step 5: if (j<=i), if not go to step 8
Step 6: Print *
Step 7: j(j+1), go to step 5
Step 8: Print on new line
Step 9: i(i+1), go to step 4
Step 10: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
printf(Enter your limit:);
scanf(%d,&n);
for(i=1;i<=n;i++)
{
Department of Computer Science & Engineering

Page 58

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 59

CS010 307(P): Programming Lab

for(j=1;j<=i;j++)
{
printf(*\t);
}
printf(\n);
}
getch();
}
RESULT
The C program to print the given pattern is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 60

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 61

CS010 307(P): Programming Lab

EXP NO: 18
DATE: 22/08/2014
BUBBLE SORT
AIM
To write a C program to perform bubble sort.
ALGORITHM
Step 1: Start
Step 2: Read n, i0, j0
Step 3: Read n values
Step 4: if (i<n), if not go to step 10
Step 5: if (j<n-1), if not go to step 9
Step 6: if (a[j]>a[j+1]), if not go to step 8
Step 7: ta[j]
a[j]a[j+1]
a[j+1]t
Step 8: jj+1, go to step 5
Step 9: ii+1, go to step 4
Step 10: Print n values
Step 11: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,t,j,a[20];
printf(Enter your limit:);
scanf(%d,&n);
printf(\nEnter your array elements:\n);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
Department of Computer Science & Engineering

Page 62

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 63

CS010 307(P): Programming Lab

}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf(\nSorted Array:\n);
for(i=0;i<n;i++)
{
printf(%d\n,a[i]);
}
getch();
}
RESULT
The C program to perform bubble sort is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 64

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 65

CS010 307(P): Programming Lab

EXP NO: 19
DATE: 22/08/2014
SELECTION SORT
AIM
To write a C program to perform selection sort.
ALGORITHM
Step 1: Start
Step 2: Read n, i0, j(i+1)
Step 3: Read n values
Step 4: if (i<n), if not go to step 10
Step 5: if (j<n), if not go to step 9
Step 6: if (a[i]>a[j]), if not go to step 8
Step 7: ta[i]
a[i]a[j]
a[j]t
Step 8: jj+1, go to step 5
Step 9: ii+1, go to step 4
Step 10: Print n values
Step 11: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,t,a[20];
printf(Enter your limit:);
scanf(%d,&n);
printf(\nEnter your array elements:\n);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
Department of Computer Science & Engineering

Page 66

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 67

CS010 307(P): Programming Lab

}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if (a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf(\nSorted array(selection sort):\n);
for(i=0;i<n;i++)
{
printf(%d\n,a[i]);
}
getch();
}
RESULT
The C program to perform selection sort is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 68

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 69

CS010 307(P): Programming Lab

EXP NO: 20
DATE: 22/09/2014
SINE SERIES
AIM
To write a C program to print sine series.
ALGORITHM
Step 1: Start
Step 2: Read x, n, k1, i3, j1, t0, a1, d0, s0, r0
Step 3: r((3.14*x)/180)
Step 4: Print Sine Series r
Step 5: sr
Step 6: if (n<2), if not go to step 8
Step 7: Print Sum s, go to step 17
Step 8: if (j<n), if not go to step 16
Step 9: a1
Step 10: if (k<=n), if not go to step 11
a(a*k)
k(k+1), go to step 10
Step 11: t(ri)
Step 12: d(t/a)
Step 13: c(-1*d)j
Step 14: Print c
Step 15: s(s+c)
i(i+2)
j(j+1), go to step 8
Step 16: if (n>1), if not go to step 17
Print Sum s
Step 17: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
Department of Computer Science & Engineering

Page 70

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 71

CS010 307(P): Programming Lab

void main()
{
int x,n,f,i=3,j,a=1;
float s,d,t;
printf("Enter value of x & n\n");
scanf("%d%d",&x,&n);
d=x*3.1415/180;
s=d;
printf("Series:\t%f",s);
while(i<=n)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
a=a*-1;
t=(pow(d,i))*a/f;
printf(" + %f",t);
s=s+t;i=i+2;
}
printf("\nSum = %f",s);
getch();
}

RESULT
The C program to print sine series is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 72

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 73

CS010 307(P): Programming Lab

EXP NO: 21
DATE: 22/09/2014
COSINE SERIES
AIM
To write a c program for finding the cosine series up to a limit.
ALGORITHM
Step 1:Start
Step 2:Declare variables i,n,j,x,t,sum,k,v as float
Step 3:Input and read the limit of the cosine series,n.
Step 4:Input and read the value of angle(in degree),x.
Step 5:Convert the angle in degree to radian,k=((x*3.1415)/180)
Step 6:Initialize t as 1,s as 1 and i as 1, j as 1.
Step 7:(j<=v) repeat the steps 8 to 10.
Step 8:Find t=(t*(-1)*r*r)/(i*(i+1))
Step 9:Find s=s+t
Step 10:Increment i by 1, Increment j by 1,Increment v by 2
Step 11:Print the sum of the cosine series,s.
Step 12:Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,n,sum=1,f=1,t,a=1,i,j,k,v;
printf("enter the values of x&n\n");
scanf("%f%f",&x,&n);
k=(x*3.14)/180;
printf("%f ",f);
v=2;
Department of Computer Science & Engineering

Page 74

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 75

CS010 307(P): Programming Lab


for(i=1;i<n;i++)
{
f=1;
for(j=1;j<=v;j++)
{
f=f*j;
}
a=a*-1;
t=(pow(k,v)*a)/f;
sum=sum+t;
printf(" %f",t);
v=v+2;
}

printf("\n the value=%f",sum);


}

RESULT
The C program to print cosine series is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 76

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 77

CS010 307(P): Programming Lab

EXP NO: 22
DATE: 29/08/2014

EXPONENTIAL SERIES

AIM
To write a program to find print exponential series.
ALGORITHM
Step 1: Start
Step 2: Declare variables i,n,j,ch as integers ,x,t,s,r as float
Step 3Input and read the limit of the exponential series,n.
Step 4:Input and read the value of power of exponential series,x.
Step 5: Initialize t as1 and s as 1.
Step 6: Initialize i as 1.
Step 7:while(i<n) repeat the steps a to c.
Find t=(t*x)/i
Find s=s+t
Increment i by 1.
End while.
Step 8: Print the sum of the exponential series,s.
Step 9: Stop

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float i,j, x,n,s,r,a=1,f=1,t;
Department of Computer Science & Engineering

Page 78

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 79

CS010 307(P): Programming Lab

printf("enter value of n\n");


scanf("%f",&n);
printf("enter value of x\n");
scanf("%f",&x);
s=1;
printf("1");
for(i=1;i<n;i++)
{
for(j=1;j<=i;j++)
{
f*=j;
}
t=pow(x,i)/f;
s+=t;
printf("+%f",t);
}
printf("\nsum of exponental series is\n %f",s);
getch();
}

RESULT
The C program for exponential series is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 80

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 81

CS010 307(P): Programming Lab

EXP NO:23
DATE:29/08/2014
MATRIX ADDITION
AIM
To write a C program to find the sum of two matrices.
ALGORITHM
1.Start.
2.Read a[20[20],b[20][20],c[20][20],i,j
3.Read the order of first matrix,m,n
4.Read the order of second matrix,p,q
5.if m==p and n==q,else goto step 16
6.Read the elements of first and second matrix
7.Print first and second matrix
8.i1
9.while (im),else goto step 14
10.j1
11.while(jn),else goto step 13
12.c[i][j]a[i][j]+b[i][j]
13.jj+1
14.ii+1,goto step 8
15.print c[i][j]
16.print matrix cannot be added.
17.stop.
PROGRAM
#include<stdio.h>
Department of Computer Science & Engineering

Page 82

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 83

CS010 307(P): Programming Lab

#include<conio.h>
#include<process.h>
main()
{
int a[20][20],b[20][20],c[20][20],i,j,m,n,p,q;
printf("enter the order of first matrix,m,n\n");
scanf("%d%d",&m,&n);
printf("enter the order of second matrix,p,q\n");
scanf("%d%d",&p,&q);
if((m!=p) || (n!=q))
{
printf("the matrices cannot be added\n");
exit(0);
}
printf("enter the elements of first array\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of second array \n");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
scanf("%d",&b[i][j]);
}
printf(" first array\n");
for(i=1;i<=m;i++)
Department of Computer Science & Engineering

Page 84

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 85

CS010 307(P): Programming Lab

{
for(j=1;j<=n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf(" second array \n");
for(i=1;i<=p;i++)
{
printf("\n");
for(j=1;j<=q;j++)
{
printf("%d\t",b[i][j]);
}
}
if((m==p)&&(n==q))
{
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
} printf("%d\t",c[i][j]);
printf("\n");
}
printf("the sum is :\n");
for(i=1;i<=m;i++)
{
printf("\n");
Department of Computer Science & Engineering

Page 86

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 87

CS010 307(P): Programming Lab

for(j=1;j<=n;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}
RESULT
The C program to find the sum of two matrices is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 88

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 89

CS010 307(P): Programming Lab

EXP NO:24
DATE:29/08/2014
MATRIX TRANSPOSE
AIM
To write a C program to find the transpose of a matrix.
ALGORITHM
1.Start
2.Read the order of matrix,m,n
3.Read the matrix a[20][20],b[20][20]
4.i1
5.Repeat steps 6 to 10 while im
6.j0
7.Repeat steps 8 and 9 while jn
8.b[i][j]a[j][i]
9.jj+1
10.ii+1
11.print matrix b
12.Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,m,n,a[20][20],b[20][20];
printf("enter the order of the array\n");
scanf("%d%d",&m,&n);
Department of Computer Science & Engineering

Page 90

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 91

CS010 307(P): Programming Lab

printf("enter the elements of array\n");


for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("the element in a[%d][%d]\n",i,j);
scanf("%d",&a[i][j]);
}
}
printf(" matrix is \n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
b[i][j]=a[j][i];
}
}
printf("the transposed matrix is :\n");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=n;j++)
{
Department of Computer Science & Engineering

Page 92

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 93

CS010 307(P): Programming Lab

printf("%d ",b[i][j]);
}
printf("\n");
}
getch();
}
RESULT
The C program to find the transpose a matrix is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 94

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 95

CS010 307(P): Programming Lab

EXP NO:25
DATE:29/08/2014
MATRIX MULTIPLICATION
AIM
To write a C program to find the product of two matrices.
ALGORITHM
1.Start.
2.Read a[20[20],b[20][20],x[20][20],i,j,k
3.Read the order of first matrix,m,n
4.Read the order of second matrix,p,q
5.Repeat steps 6 to while p==n,else goto step 20
6.Read the elements of first and second matrix.
7.Print first and second matrix.
8.i0
9.Repaet steps 10 to 18 while i<m
10.j0
11.Repeat steps 12 to 17 while j<q
12.x[i][j]0
13.k0
14.repeat steps 15 to 16 while k<p
15.x[i][j]x[i][j]+(a[i][k]*b[k][j])
16.kk+1
17.jj+1
18.ii+1
19.Print x[i][j]
20.Print matrix cannot be multiplied.
21.Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
Department of Computer Science & Engineering

Page 96

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 97

CS010 307(P): Programming Lab

{
int i,j,k,m,n,p,q,a[10][10],b[10][10],x[10][10];
printf("enter the order of first matrix \n");
scanf("%d%d",&m,&n);
printf("enter the order of second matrix\n");
scanf("%d%d",&p,&q);
if(n!=p || m!=q)
{
printf("matrices cannot be multiplied\n");
}
if(p==n)
{
printf("enter the elements of first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf(" first matrix\n");
for(i=0;i<m;i++)
{
Department of Computer Science & Engineering

Page 98

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 99

CS010 307(P): Programming Lab

for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf(" second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
x[i][j]=0;
for(k=0;k<p;k++)
{
x[i][j]=x[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("the product of two matrices is :\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
Department of Computer Science & Engineering

Page 100

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 101

CS010 307(P): Programming Lab

{
printf("%d\t",x[i][j]);
}
}
}
getch();
}
RESULT
The C program to find the product of two matrices is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 102

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 103

CS010 307(P): Programming Lab

EXP NO:26
DATE:19/09/2014
LINEAR SEARCH
AIM
To write a C program to find an element in an array using linear search.
ALGORITHM
1.Start.
2.Read a[20],n,k,i,b,flag
3.Read the elements of array
4.Read the element to be searched.
5.k0
6.Repeat steps 7 to 9 while k<n.
7.if a[k]==b,else goto step 9
8.flag=1,goto step 10
9.kk+1
10.if flag==1,else goto step 12
11.Print element is found
12.Print element is not found
13.Stop.
PROGRAM
#include<stdio.h>
main()
{
int a[20],n,b,k,i,flag=0,c=0;
printf("enter the number of elements in an array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
Department of Computer Science & Engineering

Page 104

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 105

CS010 307(P): Programming Lab

printf("enter the element to be searched\n");


scanf("%d",&b);
for(k=0;k<n;k++)
{
if(a[k]==b)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("element is found at the location %d\n",k+1);
}
else
{
printf("the element is not found\n");
}
getch();
}
RESULT
The C program to find an element in an array using linear search is completed successfully and
the output is verified.

Department of Computer Science & Engineering

Page 106

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 107

CS010 307(P): Programming Lab

EXP NO:27
DATE:19/09/14
NUMBER OF VOWELS IN A STRING
AIM
To write a C program to find the number of vowels in a string.
ALGORITHM
1.Start.
2.Read the string str,c0,i0
3.Repeat steps 4 to 6 while istrlen(str)
4.if str[i]=a,e,i,o,u or A,E,I,O,U
5.cc+1
6.ii+1
7.Print c
8.Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int i,c=0;
char str[20];
printf("enter the string\n");
gets(str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='I'||str[i]=='E'||str[i]=='
O'||str[i]=='U')
{
c++;
}
}
Department of Computer Science & Engineering

Page 108

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 109

CS010 307(P): Programming Lab

printf("the number of vowels is : %d",c);


getch();
}
RESULT
The C program to find the number of vowels in a string is completed and the output is verified.

Department of Computer Science & Engineering

Page 110

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 111

CS010 307(P): Programming Lab

EXP NO:28
DATE:19/09/14
STRING PALINDROME
AIM
To write a C program to check whether the given string is palindrome or not.
ALGORITHM
1.Start
2.Read i,j,n,l0,c1
3.Read the string str
4.i0
5.Repeat step 6 and 7 while str[i]!=\0
6.ll+1
7.ii+1
8.nl-1
9.i0
10.Repeat steps 11 to 14 while i<l/2
11.if str[i]!=str[n],else goto step 15
12.c=0
13.nn-1
14.ii+1
15.if c==1,else goto step 17
16.Print string is palindrome
17.Print string is not palindrome.
18.Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int i,l=0,c=1,j,n;
Department of Computer Science & Engineering

Page 112

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 113

CS010 307(P): Programming Lab

char str[20],str1[20];
printf("\n enter the string \n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
l++;
}
n=l-1;
for(i=0;i<l/2;i++)
{
if(str[i]!=str[n])
{
c=0;
}
n--;
}
if(c==1)
{
printf("the string is palindrome\n");
}
else
{
printf(" string is not palindrome\n");
}
getch();
}
RESULT
The C program to check whether the given string is palindrome or not is completed successfully
and the output is verified.

Department of Computer Science & Engineering

Page 114

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 115

CS010 307(P): Programming Lab

EXP NO:29
DATE:19/09/14
STRING SORTING
AIM
To write a C program to sort the given strings.
ALGORITHM
1.Start.
2.Read the limit n,str[20][20].t[20],i,j
3.Read the strings.
4.i0
5.Repeat steps 6 to 11 while in
6.jj+1
7.Repeat steps 8 to 10 while jn
8.if strcmp(str[i],str[j]>0),else goto step 10
9.Swap str[i] and str[j]
10.jj+1
11.ii+1
12.Print sorted strings str[i]
13.Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int n,i,j;
char str[20][20],t[20];
printf(" enter the limit\n ");
scanf("%d",&n);
printf("enter the strings\n");
for(i=0;i<=n;i++)
Department of Computer Science & Engineering

Page 116

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 117

CS010 307(P): Programming Lab

{
gets(str[i]);
}
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(t,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],t);
}
}
}
printf("the sorted strings are \n");
for(i=0;i<=n;i++)
{
puts(str[i]);
}
getch();
}

RESULT
The C program to sort the given strings is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 118

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 119

CS010 307(P): Programming Lab

EXP NO:30
DATE:14/10/14
HCF AND LCM OF TWO NUMBERS USING FUNCTION
AIM
To write a C program to find the hcf and lcm of two numbers using function.
ALGORITHM
1.Start
2.Read a,b,x,y,t,lcmno,hcfno
3.Print Menu
1.hcf
2.lcm
4.Read the choice ch
5.if ch=1,goto step 7
6.if ch=2,goto step 9
7.hcfno=hcf(a,b)
8.print hcfno
9.lcmno=lcm(a,b)
10.print lcmno
11.stop
Function :: hcf()
1.Read hcf,t
2. while y!=0,else goto step 7
3.ty
4.yx%y
5.xt
6.hcfx
7.return hcf
Function :: lcm()
1.Read hcf,t,lcm,p,q
2.px,qy
3. while y!=0,else goto step 9
Department of Computer Science & Engineering

Page 120

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 121

CS010 307(P): Programming Lab

4.ty
5.yx%y
6.xt
7.hcfx
8.lcm(p*q)/hcf
9.return lcm
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<process.h>
int hcf(int,int);
int lcm(int,int);
main()
{
int a,b,x,y,t,lcmno,hcfno,ch;
printf(" enter the values\n");
scanf("%d%d",&a,&b);
do
{
printf(" \n1.HCF\n2.LCM\n");
printf(" Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: hcfno=hcf(a,b);
printf("HCF = %d,hcfno);
break;
case 2 : lcmno=lcm(a,b);
printf(" LCM = %d",lcmno);
break;
default : exit(0);
Department of Computer Science & Engineering

Page 122

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 123

CS010 307(P): Programming Lab

break;
}
}
while(ch!=3);
getch();
}
int hcf(int x,int y)
{
int hcf,t;
while(y!=0)
{
t=y;
y=x%y;
x=t;
}
hcf=x;
return(hcf);
}
int lcm(int x,int y)
{
int hcf,t,lcm,p,q;
p=x;
q=y;
while(y!=0)
{
t=y;
y=x%y;
x=t;
}
hcf=x;
lcm=(p*q)/hcf;
Department of Computer Science & Engineering

Page 124

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 125

CS010 307(P): Programming Lab

return(lcm);
}
RESULT
The C program to find the hcf and lcm of two numbers using function is completed successfully
and the output is verified.

Department of Computer Science & Engineering

Page 126

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 127

CS010 307(P): Programming Lab

EXP NO:31
DATE:14/10/14
FACTORIAL OF A NUMBER USING RECURSION
AIM
To write a C program to find the factorial of a number using recursion.
ALGORITHM
Step 1: Start
Step 2: Read n,f
Step 3: Pass n, +0 factorial(int)
Step 4: f1
Step 5: if x==1, return f, else goto step 5
Step 6: f=f*factorial(x-1)
Step 7: Display f
Step 8: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
int fact(int);
main()
{
int n,fact;
printf(" enter a number \n");
scanf("%d",&n);
fact=factorial(n);
printf("factorial = %d",fact);
getch();
}
int factorial(int x)
{
int fact;
if(x==1)
Department of Computer Science & Engineering

Page 128

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 129

CS010 307(P): Programming Lab

{
return 1;
}
else
{
fact=x*factorial(x-1);
return (fact);
}
}
RESULT
The C program to find the factorial of a number is completed successfully and the output
is verified.

Department of Computer Science & Engineering

Page 130

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 131

CS010 307(P): Programming Lab

EXP NO:32
DATE:14/10/14
MACRO
AIM
To write C program to find the square, cube, fourth and fifth power of a number using Macro.
ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: s=sq(a)
Step 4: c=cu(a)
Step 5: fo=fo(a)
Step 6: fi=fi(a)
Step 7: Print values
Step 8: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#define sq(x)(x*x)
#define cu(x)(sq(x)*x)
#define fo(x)(cu(x)*x)
#define fi(x)(fo(x)*x)
void main()
{
int a,s=0,c,fo,fi;
printf("Enter the number:");
scanf("%d",&a);
s=sq(a);
c=cu(a);
fo=fo(a);
fi=fi(a);
printf("\n The square is %d",s);
Department of Computer Science & Engineering

Page 132

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 133

CS010 307(P): Programming Lab

printf("\n The cube is %d",c);


printf("\n The fourth power is %d",fo);
printf("\n The fifth power is %d",fi);
getch();
}
RESULT
The C program to find the square, cube, fourth and fifth power of a number using Macro is
completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 134

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 135

CS010 307(P): Programming Lab

EXP NO:33
DATE:14/10/14
STUDENT DETAILS USING STRUCTURE
AIM
To write a C program to store student details using structure.
ALGORITHM
Step 1: Start
Step 2: k=0
Step 3: struct student
Step 4: Read an array using the structure variable st[30]
Step 5: Read the number of students=n
Step 6: while(k<n) repeat the following steps
Step 7: Read the name of first student st[k].name
Step 8: Read the roll no of first student st[k].rollno
Step 9: k+1
Step 10: Print the details of student
Step 11: Stop
PROGRAM
#include<stdio.h>
void main()
{
struct student
{
char name[15];
int rollno;
int mark;

}s;
printf("enter the name,rollno and total marks out of 100:\n");
scanf("%s%d%d",&s.name,&s.rollno,&s.mark);
printf("the student details are\n");
Department of Computer Science & Engineering

Page 136

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 137

CS010 307(P): Programming Lab

printf("Rollno: %d\n name:%s\n Total marks out of 100:%d",s.rollno,s.name,s.mark);


getch();
}

RESULT
The C program to store student details using structure is completed successfully
and the output is verified.

Department of Computer Science & Engineering

Page 138

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 139

CS010 307(P): Programming Lab

EXP NO:34
DATE:14/10/14
STRUCTURE SORTING
AIM
To write a C program to store student details using structure and to sort it.
ALGORITHM
Step 1: Start
Step 2: Declare the structure
Step 3: Open the void main and read the values i, j, n
Step 4: Read and display the details to be sorted
Step 5: Sorting steps are: i1,j0, i<n, j<n-1, then compare the ones to be sorted
Step 6: if strcmp(s[j].name, s[j+1].name)>0)
temp=s[j]
s[j]=s[j+1]
s[j+1]=temp
Step 7: Print the sorted structure
Step 8: Stop
PROGRAM
#include<stdio.h>
#include<string.h>
void main()
{
struct student
{
char name[20];
int rollno;
}s[20],t;
int i,n,j,k;
printf("Enter the limit:\n");
scanf("%d",&n);
printf("Enter the name and Roll No of %d students :\n",n);
Department of Computer Science & Engineering

Page 140

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 141

CS010 307(P): Programming Lab

for(i=0;i<n;i++)
{
scanf("%s%d",s[i].name,&s[i].rollno);
}
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
for(j=0;s[i].name[j]!='\0';j++)
{
if(s[i].name[j]==s[k].name[j])
{
continue;
}
if(s[i].name[j]>s[k].name[j])
{
t=s[i];
s[i]=s[k];
s[k]=t;
break;
}
else
break;
}
}
}
printf("After sorting\n");
for(i=0;i<n;i++)
printf("\n name :%s\n rollno:%d",s[i].name,s[i].rollno);
getch();
}
Department of Computer Science & Engineering

Page 142

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 143

CS010 307(P): Programming Lab

RESULT
The C program to store student details using structure and to sort is completed
successfully and the output is verified.

Department of Computer Science & Engineering

Page 144

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 145

CS010 307(P): Programming Lab

EXP NO:35
DATE:24/10/14
SWAPPING TWO NUMBERS USING CALL BY REFERENCE
AIM
To write C program to swap two numbers using call by reference method.
ALGORITHM
1.Start
2.Read a,b
3.swap(a,b)
4.Print a,b
5.Stop
Function :: swap()
1.Read c
2.c=*a
3.*a=*b
4.*b=c
5.Return.
PROGRAM
#include<stdio.h>
#include<conio.h>
void swap(int*,int*);
main()
{
int a,b;
printf(" enter two numbers \n");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("the swaped numbers are\n%d \n%d \n",a,b);
getch();
}
void swap(int *x,int *y)
Department of Computer Science & Engineering

Page 146

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 147

CS010 307(P): Programming Lab

{
int z;
z=*x;
*x=*y;
*y=z;
return(x,y);
}
RESULT
The C program to swap two numbers using call by reference is completed successfully
and the output is verified.

Department of Computer Science & Engineering

Page 148

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 149

CS010 307(P): Programming Lab

EXP NO:36
DATE:24/10/14
POINTER ARITHMETIC
AIM
To write C program for Point Arithmetic.
ALGORITHM
Step 1: Start
Step 2: Read numbers a and b
Step 3: opera(a,b)
Step 4: Stop
FUNCTION :: opera()
Step 1: int p, q, r, s
Step 2: x=a, y=b
Step 3: p=x+y
Step 4: print p
Step 5: q=x-y
Step 6: print q
Step 7: r=x*y
Step 8: print r
Step 9: s=x/y
Step10: print s
PROGRAM
#include<stdio.h>
#include<conio.h>
void arithmetic (int,int,int*,int*,int*,int*);
void main()
{
int x,y,s,d,m,div;
printf("Enter two numbers : \n");
scanf("%d%d",&x,&y);
arithmetic(x,y,&s,&d,&m,&div)
Department of Computer Science & Engineering

Page 150

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 151

CS010 307(P): Programming Lab

printf("Sum =%d\n Difference=%d\n Product =%d\n Quotient=%d\n",s,d,m,div);


getch();
void arithmetic (int p,int q,int*s,int*d,int*m,int*div)
{
*s=p+q;
*d=p-q;
*m=p*q;
*div=p/q;
}
RESULT
The C program for Pointer Arithmetic is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 152

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 153

CS010 307(P): Programming Lab

EXP NO:37
DATE:24/10/14
ARRAY SORTING USING POINTERS
AIM
To write C program for Array Sorting using Pointers.
ALGORITHM
Step 1: Start
Step 2: Read the pointer array and array elements
Step 3: Read the array elements
Step 4: Sort using array pointer
Step 5: Stop
PROGRAM
#include<stdio.h>
void main()
{
int a[10],i,j,t,n,*ptr;
printf("Enter the limit:\n");
scanf("%d",&n);
printf("Enter the array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
ptr=a;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(*(ptr+i)>*(ptr+j))
{
t=a[i];
Department of Computer Science & Engineering

Page 154

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 155

CS010 307(P): Programming Lab

a[i]=a[j];
a[j]=t;
}
}
}
printf("\n\n Sorted Array:\n");
for(i=0;i<n;i++)
printf(" %d",a[i]);
getch();
}
RESULT
The C program for Array Sorting using Pointers is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 156

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 157

CS010 307(P): Programming Lab

EXP NO:38
DATE:25/10/14

FILE PROGRAM
AIM
To write C program for File Program.
ALGORITHM
Step 1: Start
Step 2: Declare the pointers fp1, fp2, fp3
Step 3: Open first file pointer and enter the values to the file and close the file.
Step 4: Open the file NUMBER in read mode and read the numbers.
Step 5: If the number %2==0, then write the file number to file EVEN, else write to file ODD
and close both files.
Step 6: Open the two files in read mode and print its values
Step 7: Stop
PROGRAM
#include<stdio.h>
void main()
{
FILE *f1,*f2,*f3;
int i,num,n;
printf("Enter the limit:\n");
scanf("%d",&n);
printf("Enter the number: \n");
f1=fopen("data","w");
for(i=0;i<n;i++)
{
scanf("%d",&num);
putw(num,f1);
}
fclose(f1);
f1=fopen("data","r");
Department of Computer Science & Engineering

Page 158

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 159

CS010 307(P): Programming Lab

f2=fopen("even","w");
f3=fopen("odd","w");
while((num=getw(f1))!=EOF)
{
if(num%2==0)
putw(num,f2);
else
putw(num,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("even","r");
f3=fopen("odd","r");
printf("contents of even file");
while((num=getw(f2))!=EOF)
printf("\n %d",num);
printf("\n content of odd file");
while((num=getw(f3))!=EOF)
printf("\n %d",num);
fclose(f2);
fclose(f3);
getch();
}
RESULT
The C program for File Program is completed successfully and the output is verified.

Department of Computer Science & Engineering

Page 160

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 161

CS010 307(P): Programming Lab

EXP NO: 39
DATE: 25/10/2014

DYNAMIC MEMORY ALLOCATION


AIM
To write C program to perform Dynamic Memory Allocation.
ALGORITHM
Step 1: Start
Step 2: Declare NULL=0 and pointer variable *b
Step 3: if(b=char*) malloc(10))=0, then print FAILED
Step 4: Enter size of the block and enter the string APPLE and print.
Step 5: The reallocate the string b, with size 15.If the b=(char*) ie, alloc (b,15)=0,the print failed.
Step 6: Print the block size and enter the strings ORANGEGRAPES.
Step 7: The free the pointer, free(p)
Step 8: Stop
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
void main()
{
char *b;
if ((b=(char*)malloc(10))==NULL)
{
printf("FAILED");
exit(1);
}
printf("Size of block created is %d \n",_msize(b));
strcpy(b,"APPLE");
printf("content in the block \n");
puts(b);
if((b=(char*)realloc(b,15))==NULL)
Department of Computer Science & Engineering

Page 162

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 163

CS010 307(P): Programming Lab

{
printf("Failed");
exit(1);
}
printf("New block size %d\n",_msize(b));
strcpy(b,"ORANGEGRAPES");
printf("new contents \n");
puts(b);

}
RESULT
The C program to perform Dynamic Memory Allocation is completed successfully and the
output is verified.

Department of Computer Science & Engineering

Page 164

CS010 307(P): Programming Lab

OUTPUT

Department of Computer Science & Engineering

Page 165

CS010 307(P): Programming Lab

EXP NO: 40
DATE: 25/10/2014

COMMAND LINE ARGUMENT


AIM
To write C program to perform Command Line Argument.
ALGORITHM
Step 1: Start
Step 2: Declare the file pointer *fp1, *fp2, *fp3
Step 3: If(argc==3), the open file INPUT in write mode.
Step 4: Read the string and write it to the file input and close the file.
Step 5: Open the file input in read mode and argv[1] and argv[2] in write mode.
Step 6: Read each character of string, if x of the INPUT is a vowel , enter the value of
argv[1],enter it to argv[2]
Step 7: Then read the data of argv[1] and argv[2] and print
Step 8: Stop
PROGRAM
#include<stdio.h>
void main(int argc,char *argv[])
{
char ch,a[80];
int i;
FILE *fptr,*fptr1;
if(argc!=3)
{
printf("\n nargc=%d\n",argc);
printf("\n insufficient command line argument\n");
}
else
{
fptr=fopen(argv[1],"w");
printf("Enter a string:");
Department of Computer Science & Engineering

Page 166

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 167

CS010 307(P): Programming Lab

gets(a);
for(i=0;a[i]!='\0';i++)
{
putc(a[i],fptr);
}
fclose(fptr);
fptr=fopen(argv[1],"r");
if(!fptr)
{
printf("can`t open the file");

}
else
{
fptr1=fopen(argv[2],"w");
if(!fptr1)
printf("can`t open the source file");
else
{
do

{
ch=getc(fptr);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
Department of Computer Science & Engineering

Page 168

CS010 307(P): Programming Lab

Department of Computer Science & Engineering

Page 169

CS010 307(P): Programming Lab

case 'o':
case 'U':
putc(ch,fptr1);
break;
}
}
while(ch!=EOF);
}
fclose(fptr);
fclose(fptr1);
fptr1=fopen(argv[2],"r");
printf("\n data from output file");
do
{
ch=getc(fptr1);
printf("%c",ch);

}
while(ch!=EOF);
}
}
return 0;
}
RESULT
The C program to perform Command Line Argument is completed successfully and the output is
verified.

Department of Computer Science & Engineering

Page 170

Vous aimerez peut-être aussi