Professional Documents
Culture Documents
1
21.
Program no. 1
//* Program to use function by finding the area of circle and rectangle *//
#include<iostream.h>
#include<conio.h>
float area;
void cir_ar() //fnction for area of circle
{
float area;
float r;
cout<<”\n Enter the radius of the circle : ";
cin>>r;
area = 3.14 * r * r;
cout<<”\n The area of the circle of radius ”<<r<<” is ”<<area;
}
void rec_ar() //fnction for area of rectangle
{
float l,b;
cout<<”\n Enter the length and breadth of a rectangle : ”;
cin>>l>>b;
area=l*b;
cout<<”\n The area of the rectangle is ”<<area;
}
void main ()
{
clrscr();
cir_ar();
rec_ar()
getch();
}
2
3
Output of Program no. 1
Program no. 2
//* Program to use function by finding the simple interest and amount using function *//
#include<iostream.h>
#include<conio.h>
void amt() //function body
{
float si;
int tm;
float rt;
int pr;
cout<<”\n Enter the Principal sum : ”;
cin>>pr;
cout<<”\n Enter the rate : ”;
cin>>rt;
cout<<”\n Enter the time( in yers ) : ”;
cin>>tm;
4
si=(pr*tm*rt);
cout<<”\n The simple interest so made is ”<<si;
cout<<”\n The total amount so made is ”<pr+si;
}
void main ()
{
clrscr();
amt(); //call of function
getch();
}
5
Output of Program no. 2
6
Program no. 3
//* Program to calculate area of a triangle or rectangle or square or triangle for the illustration of
function overloading. *//
#include<iostream.h>
#include<conio.h>
#include<math.h> //for sqrt function
#include<process.h> //for sqrt function
float area (float a, float b, float c) //1 function for area of triangle
{
float s, ar;
s = (a+b+c)/2;
ar = sqrt(s*(s-a)*(s-b)*(s-c));
reurn ar;
}
float area(float a, float b) //2 function for area of rectangle
{
return a*b;
}
float area(float a) //3 function for area of square
{
return a*a;
}
void main ()
{
clrscr();
int ch;
float s1,s2,s3,ar;
cout<<”\n Menu for Area \n”;
cout<<”\n 1. Tirangle \n 2. Rectangle \n 3. Square \n 4. Exit ”;
cout<<"\n Enter your choice : ";
cin>>ch;
switch(ch);
{
case '1’ : cout<<"\n Enter the three sides of the triangle : ";
cin>>s1>>s2>>s3;
ar=area(s1,s2,s3); //call of function1
cout<<"\n The Area of the Triangle is "<<ar;
7
case '2’ : cout<<"\n Enter the length and breadth of the
rectangle : ";
cin>>l>>b;
ar=area(l,b); //call of function2
cout<<"\n The Area of the Rectangle is "<<ar;
case '3’ : cout<<"\n Enter the side of the square : ";
cin>>s;
ar=area(s); //call of function3
cout<<"\n The Area of the Rectangle is "<<ar;
}
getch();
}
8
Program no. 4
//* Program to read and write students’ details using file
handling *//
Program no. 5
//* Program to calculate the percentage and print details of n students *//
#include<iostream.h>
#include<conio.h>
class student
{
Int roll, t,avg;
char name[20],grade;
9
int cls;
int mrks[5];
float percent;
float calc();
public :
void readmrks();
void dispmrks();
};
float student::calc()
{
percnt=0;
for ( int i=0; i<3; i++ )
{
percnt+=mrks[i];
percnt=(percnt/3);
}
return percnt;
}
void student :: readstudent()
{
cout<<”\n Enter the name of the student : ”;
cin>>name;
cout<<”\n Enter the class of the student : ”;
cin>>cls;
cout<<”\n Enter the roll no. of the student : ”;
cin>>roll;
cout<<”\n Enter the marks in the 5 subjects of the student : ”;
for ( int j =0; i<4; j++ )
cin>>mrks[j];
}
void student :: readstudent()
{
cout<<”\n Name : ”<<name;
cout<<”\n Class : ”<<cls;
cout<<”\n Roll no. : ”<<roll;
cout<<”\n Percentage : ”<<calc(); //call of function
}
void main ()
{
clrscr();
10
int i, n;
student s1[5];
cout<<”\n Enter the number of students : ”;
cin>>n;
for ( i=1; i<=5; i++ )
{
s1[i].readmrks(); //call of function
s1[i].dispmrks(); //call of function
}
getch();
11
Output of Program no. 5
12
Program no. 6
//* Program to find the area of rectangle using constructor*//
Program no. 7
//* Prgram to enter and show student record using constructor and destructor *//
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
char name[10],add[10];
int roll;
public :
void in();
void out();
student();
~student();
};
student :: student()
{
cout<<"\n STUDENT RECORD \n";
}
void student : : in ()
{
cout<<"\n Enter student name : ";
cin>>name;
cout<<"\n Enter student roll no. : ";
cin>>roll;
cout<<"\n Enter student address : ";
cin>>add;
cout<<"\n Enter the pin code : ";
cin>>pin;
13
}
void student : : out ()
{
cout<<"\n Student Name : ";
puts(name);
cout<<"\n Student Roll No. : ";
puts(roll);
cout<<"\n Student Address : ";
puts(add);
cout<<"\n PIN CODE :";
puts(pin);
}
student : : ~ student()
{
cout<<"\n END of the file...... ";
}
student stu;
void main ()
{
clrscr();
stu.in (); //call of object and function
stu.out(); //call of object and function
getch();
}
14
15
Output of Program no. 7
Program No. 8
//* Program to Display multiplication of i numbers using array *//
#include<iostream.h>
#include<conio.h>
Void main ()
{ clrscr()
Int a[10],I;
double ttl = 1;
cout<<”\n No. of element stored in array : ”;
cin >>I;
cout <<”\n Enter the elements of the arry : \n” ;
16
for ( int j = 0 ; j,<I; j++)
{
ttl =ttl*a[i];
}
cout <<”/n The multiplication of the elements of array is
“<<ttl<<\n”;
getch();
}
17
Program No. 9(i)
//* Program to Add two matrices *// //* Program to add two matrices *//
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main ()
{
clrscr();
int a[10][10], b[10][10], c[10][10];
int i,j,m,n,p,q; //Input rows and columns of matrices A[m][n] and B[p][q]
cout<<"\n Input rows and columns of matrix-A ";
cin>>m>>n;
cout<<"\n Input rows and columns of matrix-B ";
cin>>p>>q;
if ( (m==p) && (n==q) ) //check for addition of matrices
cout<<"\n Matrixes can be added. ";
else
{
cout<<"\n Matrixes can't be added. ";
18
exit(0);
}
cout<<"\n Input matrix-A \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\n Input matrix-B \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
cout<<"\n Matrix - A \n";
for(i=0;i<m;i++)
{ cout<<"\n";
for(j=0;j<n;j++)
{
cout<<a[i][j];
}
}
cout<<"\n Matrix - B \n";
for(i=0;i<m;i++)
{ cout<<"\n";
for(j=0;j<n;j++)
{
cout<<b[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
19
}
cout<<"\n The sum of the two matrices is ";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<< c[i][j];
}
}
getch();
}
20
21
Output of Program No. 9(i)
22
Program No. 9(ii)
//* Program to Subtract two matrices *//
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main ()
{
clrscr();
int a[10][10], b[10][10], c[10][10];
int i,j,m,n,p,q; //Input rows and columns of matrices A[m][n] and B[p][q]
cout<<"\n Input rows and columns of matrix-A ";
cin>>m>>n;
cout<<"\n Input rows and columns of matrix-B ";
cin>>p>>q;
if ( (m==p) && (n==q) ) //check for addition of matrices
cout<<"\n Matrixes can be added. ";
else
{
cout<<"\n Matrixes can't be added. ";
exit(0);
}
cout<<"\n Input matrix-A \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"\n Input matrix-B \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
23
cout<<"\n Matrix - A \n";
for(i=0;i<m;i++)
{ cout<<"\n";
for(j=0;j<n;j++)
{
cout<<a[i][j];
}
}
cout<<"\n Matrix - B \n";
for(i=0;i<m;i++)
{ cout<<"\n";
for(j=0;j<n;j++)
{
cout<<b[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\n The sum of the two matrices is ";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<< c[i][j];
}
}
getch();
}
24
25
26
Output of Program No. 9(ii)
28
29
Output of Program No. 9(iii)
30
Program No. 10
//* Program to Find transpose of a matrix. *//
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr ();
int a[10][10], b[10][10], I, j, m, n;
cout<<"\n Enter row and matrix of matrix-A : ";
cin>>m>>n; //inputs of rows and columns no.
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<”\n Matrix – A : \n”; //displaying matrix
for(i=0;i<m;i++)
{
31
for(j=0;j<n;j++)
{
cout<<a[i][j]<<” ”;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[j][i];
}
}
cout<<”\n Transpose of matrix is \n”;
for(i=0;i<m;i++) //loop to print transpose
{
for(j=0;j<n;j++)
{
cout<<b[j][i]<<” ”;
}
}
getch();
}
32
Output of Program No. 10
33
Program No. 11(i)
//* Program to Search a number using linear search*//
//* Program to illustrate linear search using array *//
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,ele,n,c=0,pos;
cout<<"\n Enter the array : \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n Enter the element to be searched : ";
cin>>ele;
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
c=1;
pos=i+1;
break;
}
}
if (c==0)
{
cout<<"\n Element not found ";
}
else
{
cout<<"\n Element found a position "<<pos;
}
getche();
}
34
Output of Program No. 11(i)
35
Program No. 11(ii)
//* Program to search a number using binary search*//
//* Program to illustrate binary search using array *//
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr ();
int n,i,a[20],item,p,q,r;
cout<<"\n Enter number of elements : ";
cin>>n;
cout<<"\n Enter the array elements : \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n Enter the element to be searched : ";
cin>>item;
p=0;
r=n-1;
q=(p+r)/2;
while (p<=r)
{ if(a[q]<item)
{
p=q+1;
}
else if (a[q]==item)
{
cout<<"\n Eement "<<item<<" is found at position "<<q+1;
break;
}
else
{
r=q-1;
}
q=(p+r)/2;
}
if (p>r)
36
{
cout<<"\n Not found..."<<item<<"is not present in the array";
}
getche ();
}
37
Output of Program No. 11(ii)
Program No. 12
//* Program to arrange some given values / numbers using bubble sort *//
//* Program to illustrate bubble sort in array *//
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr ();
int a[20],n,i,j,temp;
cout<<"\n Enter the size of the array (max.20) : ";
cin>>n;
cout<<"\n Enter the array elements : \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
38
for(i=1;i<n;++i)
{
for(j=1;j<(n-i);++j)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\n Array after sorting: \n ";
for(i=0;i<n;++i)
{
cout<<" "<<a[i];
}
getche ();
}
39
Output of Program No. 12
40
Program No. 13
//* Program to Sort the given array by using deletion sort *//
//* Program to illustrate deletion sort using array *//
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr();
int i,a[10],no,pos,n;
cout<<"\n Enter number of elements(max.10) : ";
cin>>n;
cout<<"\n Enter the Array : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n Sorted data : \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
cout<<"\n Enter the position of the element to be deleted : ";
cin>>pos;
if(pos>n)
cout<<"\n This value is not in range. ";
else
{--pos;
for(i=pos;i<=(n-1);i++)
a[i]=a[i+1];
cout<<"\n New list of elements : \n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
getche();
}
41
42
Output of Program No. 13
Program No. 14
//* Program to Sort the given array by using merge sort*//
//* Program to illustrate merge sort in array *//
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void mrgsrt(int a[],int i,int j);
void mrg(int a[],int i1,int j1,int i2,int j2);
void main()
{
clrscr();
int a[20],n,i;
cout<<"\n Enter number of elements:";
cin>>n;
cout<<"\n Enter array elements: \n";
for(i=0;i<n;i++)
43
{
cin>>a[i];
}
mrgsrt(a,0,n-1); //call of mrgsrt()
cout<<"\n Sort ed array is ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getche();
}
void mrgsrt(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid =(i+j)/2;
mrgsrt(a,i,mid); //left recursion
mrgsrt(a,mid+1,j); //right recursion
mrg(a,i,mid,mid+1,j); //merging sorted sub arrays
}
}
void mrg(int a[],int i1,int j1,int i2,int j2)
{
int temp[20];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1&&j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
44
for(i=i1,j=j2;i<=j2;i++,j++)
a[i]=temp[j];
}
45
Output of Program No. 14
46
Program No. 15
//* Program to *//
47
48