0% found this document useful (0 votes)
606 views

C++ (CPP) Programs With Numerical Problems

The document contains code for several C++ programs that perform numerical operations and array manipulation: 1) A linear search program that searches an array for a target number. 2) A binary search program that searches a sorted array for a target number. 3) A bubble sort program that sorts an array of numbers in ascending order. 4) A quicksort program that implements the quicksort algorithm to sort an array of numbers. 5) A menu-driven program that implements various operations like adding, deleting, modifying records in an array of structures.

Uploaded by

v2brother
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, TXT or read online on Scribd
0% found this document useful (0 votes)
606 views

C++ (CPP) Programs With Numerical Problems

The document contains code for several C++ programs that perform numerical operations and array manipulation: 1) A linear search program that searches an array for a target number. 2) A binary search program that searches a sorted array for a target number. 3) A bubble sort program that sorts an array of numbers in ascending order. 4) A quicksort program that implements the quicksort algorithm to sort an array of numbers. 5) A menu-driven program that implements various operations like adding, deleting, modifying records in an array of structures.

Uploaded by

v2brother
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, TXT or read online on Scribd
You are on page 1/ 48

c++ (cpp) programs with numerical problem

..........................................................................
//Program name:Linear search
//Author name :alfred hitchcock
//Created on :27/03/10
//Compiled on :27/03/10
//Objective : To search an element in the array
#include<iostream>
using namespace std;
int main()
{
int a[50],n,i,t;
cout<<"Enter the size of the array\n";
cin>>n;
cout<<"Enter the elements\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the number to be searched\n";
cin>>t;
for(i=0;i<n&&t!=a[i];i++);
if(i<n)
cout<<"Number found\n"<<endl;
else
cout<<"Number Not found\n"<<endl;
return 0;
}

OUTPUT

Enter the size of the array


4
Enter the elements
2 5 6 7
Enter the number to be searched
6
Number found
.......................................................................
//Binary search
//program name : bin.cpp
//author name : alfred hitchcock
//Date written : 27-01-2010
//Date compiled : 27-01-2010
//Revision History
// 1.o

#include<iostream>
using namespace std;
main()
{
int a[100],i,n,g,low,h,m;
cout<<"enter no of elements\n";
cin>>n;
cout<<"Enter "<<n<<" integer elements in ascending order\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter element to be searched ";
cin>>g;
low=0;
h=n-1;
while(low<=h)
{
m=(low+h)/2;
if(g==a[m])
{
cout<<"Index is "<<m<<"\n";
break;
}
if(g>a[m])
low=m+1;
else
h=m-1;

cout<<g<<" not found\n";


}
}
OUTPUT

enter no of elements
3
Enter 3 integer elements in ascending order
1 2 3

Enter element to be searched 2


Index is 1
...........................................................................

//Program name: bubble sort


//Author name : alfred hitchcock
//Created on : 27-01-2010
//Compiled on : 27-01-2010
//Objective : To sort the array

#include<iostream>
using namespace std;
int main()
{
int i,j,n,temp;
int a[10];
cout<<"\n Enter the size of the array";
cin>>n;
cout<<"\n Enter the array elements";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
for(j=n;j>i;j--)
{
if(a[i]<a[j-1])
{
temp=a[i];
a[i]=a[j-1];
a[j-1]=temp;
}
}
cout<<"\n the sorted array is :";
for(i=0;i<n;i++)
cout<<"\n"<<a[i];
return 0;
}

OUTPUT
Enter the size of the array 5
Enter the array elements 2 5 1 3 6
the sorted array is :
6
5
3
2
1
........................................................................
//**********************************************************
//program name : quicksort.cpp
//author : alfred hitchcock
//date : 27-03-2010
//date compiled : 27-03-2010
//aim of the program : to sort the numbers by quick sort
//**********************************************************
#include<iostream>
using namespace std;
void quicksort(int[],int,int);
int partition(int[],int,int);
void swap(int[],int,int);
main()
{int a[20],n,i,j;
cout<<"enter array size";
cin>>n;
cout<<"\nenter array elements";
for(i=0;i<n;i++)
{cin>>a[i];
}
int lb=0;
int ub;
ub=n-1;
quicksort(a,lb,ub);
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
void quicksort(int a[],int lb,int ub)
{int j;
if(lb<ub)
{j=partition(a,lb,ub);
quicksort(a,lb,j-1);
quicksort(a,j+1,ub);
}
}
int partition(int a[],int lb,int ub)
{ int x=a[lb],left=lb,right=ub;
while(left<right)
{while((a[left]<=x)&&(left<ub))
left++;
while(a[right]>x)
right--;
if(left<right)
swap(a,left,right);
}
a[lb]=a[right];
a[right]=x;
return right;
}
void swap(int a[],int l,int r)
{int temp;
temp=a[l];
a[l]=a[r];
a[r]=temp;
}

OUTPUT
enter array size 3
enter array elements 2 5 1
1 2 5
........................................................................

//************************************************************************
// Program Name : Menudriven.cpp *
// executable file : Menudriven *
// Author : alfred hitchcock
*
// Date Written : 27-03-2010 *
// Date Compiled : 27-03-2010 *
// Aim of the Program : Menudriven program to process an *
// array of records *
// Version : 1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
#include<string.h>
struct record
{
char name[30];
int age;
};
record rec[5];
int main()
{
int add(int);
int del(int);
int ins(int);
void mod(int);
void sort(int);
void search(int);
int N,i;
cout<<"\nPlease enter the number of records you want to enter:";
cin>>N;
cout<<"Enter details of "<<N<<" records:";
for(i=0;i<N;i++)
{
cout<<"\nEnter name:";
cin>>rec[i].name;
cout<<"Enter age:";
cin>>rec[i].age;
}
cout<<"What do you want to do?\n1.ADD RECORD\n2.DELETE RECORD\n3.INSERT
RECORD\n4.MODIFY RECORD\n5.SORT RECORDS\n6.SEARCH A RECORD\n7.EXIT\nPlease enter
your choice as the corresponding number:";
int choice;
cin>>choice;
switch(choice)
{
case 1:
N=add(N);
cout<<"\n Total number of records after addition is "<<N
<<"\nRecords after addition are:";
for(i=0;i<N;i++)
{
cout<<"\nName:";
cout<<rec[i].name;
cout<<"Age:"<<rec[i].age;
}
break;
case 2:
N=del(N);
cout<<"\nTotal number of records after deletion is "<<N<
<"\nRecords after deletion are:";
for(i=0;i<N;i++)
{
cout<<"\nName:";
cout<<rec[i].name;
cout<<"Age:"<<rec[i].age;
}
break;
case 3:
N=ins(N);
cout<<"\nTotal number of records after insertion is "<<N
<<"\nRecords after insertion are:";
for(i=0;i<N;i++)
{
cout<<"\nName:";
cout<<rec[i].name;
cout<<"Age:"<<rec[i].age;
}
break;
case 4:
mod(N);
cout<<"\nAfter Modification...";
for(i=0;i<N;i++)
{
cout<<"\nName:";
cout<<rec[i].name;
cout<<"Age:"<<rec[i].age;
}
break;
case 5:
sort(N);
break;
case 6:
search(N);
break;
case 7:
cout<<"\nYou are going to exit";
break;
}
return (0);
}
int add(int n)
{
if (n==5)
{
cout<<"\nSorry the record data is full";
}
else
{
cout<<"\nEnter details of new record:";
cout<<"\nEnter name:";
cin>>rec[n].name;
cout<<"Enter age:";
cin>>rec[n].age;
n=n+1;
}
return (n);
}
int del(int n)
{
cout<<"\nEnter the name you want to delete:";
char a[30];
cin>>a;
int cmp;
int flag=0;
for(int i=0;i<n;i++)
{
cmp=strcmp(a,rec[i].name);
if(cmp==0)
{
cout<<"\nFound";
cout<<"\nGoing to delete record "<<i<<"\nThe details are
:";
cout<<"\nName:";
cout<<rec[i].name;
cout<<"\nAge:"<<rec[i].age;
flag=1;
for(int j=i;j<n;j++)
{
strcpy(rec[j].name,rec[j+1].name);
rec[j].age=rec[j+1].age;
}
n=n-1;
break;
}
}
if(flag==0)
cout<<"\nThe record not found";
return (n);
}
int ins(int n)
{
cout<<"\nEnter the record number at which you want to insert new one:";
int x;
cin>>x;
if(x-1>=5)
cout<<"\nThere is no space to insert";
else if(x-1>=n&&x-1<5)
{
if(n+1<5)
{
cout<<"\nEnter details of new record:";
cout<<"\nEnter name:";
cin>>rec[n].name;
cout<<"Enter age:";
cin>>rec[n].age;
n=n+1;
}
else
cout<<"There is no space to insert";
}
else
{
if(n+1<5)
{
for(int i=n;i>=x-1;i--)
{
strcpy(rec[i].name,rec[i-1].name);
rec[i].age=rec[i-1].age;
}
cout<<"\nGoing to insert record "<<x<<"\nEnter the details:";
cout<<"\nEnter name:";
cin>>rec[x-1].name;
cout<<"Enter age:";
cin>>rec[x-1].age;
n=n+1;
}
else
cout<<"\nThere is no space to insert";
}
return (n);
}
void mod(int n)
{
cout<<"\nEnter the name you want to modify:";
char a[30];
cin>>a;
int cmp;
int flag=0;
for(int i=0;i<n;i++)
{
cmp=strcmp(a,rec[i].name);
if(cmp==0)
{
cout<<"\nFound";
cout<<"\nEnter new details:";
cout<<"\nEnter name:";
cin>>rec[i].name;
cout<<"Enter age:";
cin>>rec[i].age;
flag=1;
break;
}
}
if(flag==0)
cout<<"\nThe record not found";
}
void sort(int n)
{
int temp,j,i;
char tempname[30];
cout<<"\nSorting according to age";
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(rec[i].age>rec[j].age)
{
strcpy(tempname,rec[j].name);
strcpy(rec[j].name,rec[i].name);
strcpy(rec[i].name,tempname);
temp=rec[j].age;
rec[j].age=rec[i].age;
rec[i].age=temp;
}
cout<<"\nAfter sorting...";
for(i=0;i<n;i++)
{
cout<<"\nName:";
cout<<rec[i].name;
cout<<"Age:"<<rec[i].age;
}
}
void search(int n)
{
cout<<"Enter the age:";
int flag=0,A,i;
cin>>A;
cout<<"\nSearching...";
for(i=0;i<n;i++)
if(rec[i].age==A)
{
flag=1;
break;
}
if(flag==1)
{
cout<<"\nFound";
cout<<"\nRecord No:"<<i+1;
cout<<"\nName:";
cout<<rec[i].name;
cout<<"Age:"<<rec[i].age;
}
else
cout<<"\nNot found";
}
OUTPUT

Please enter the number of records you want to enter:1


Enter details of 1 records:
Enter name:john
Enter age:25
What do you want to do?
1.ADD RECORD
2.DELETE RECORD
3.INSERT RECORD
4.MODIFY RECORD
5.SORT RECORDS
6.SEARCH A RECORD
7.EXIT
Please enter your choice as the corresponding number:1
Enter details of new record:
Enter name:jose
Enter age:27
Total number of records after addition is 2
Records after addition are:
Name:johnAge:25
Name:joseAge:27
.........................................................................
//Program name: Matrix Operations
//Author name : alfred hitchcock
//Created on : 27/03/10
//Compiled on : 27/03/10
//Objective : Matrix operations add and multiply
#include<iostream>
using namespace std;
int main()
{
int s,x=0,choice,i,j,k,a[3][3],b[3][3],c[3][3],d[3][3];
cout<<"Enter the elements of first matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"\nEnter the elements of 2nd matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>b[i][j];
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
s=a[i][k]*b[k][j];
x+=s;
d[i][j]=a[i][j]+b[i][j];
}
c[i][j]=x;
x=0;
}
}
cout<<"\n\tThe Operations on Matrices are done\n";
cout<<"\nWhich you want to see?...1.ADD MATRIX 2.MULTIPILY MATRIX \n";
cin>>choice;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(choice==2)
cout<<c[i][j];
if(choice==1)
cout<<d[i][j];
cout<<"\t";
}
cout<<"\n";
}
}

OUTPUT

Enter the elements of first matrix


1 2 3
4 5 6
7 8 9
Enter the elements of 2nd matrix
3 4 5
5 6 7
8 4 5
The Operations on Matrices are done
Which you want to see?...1.ADD MATRIX 2.MULTIPILY MATRIX
1
4 6 8
9 11 13
15 12 14
//************************
//Program name : series.cpp
//Author name : alfred hitchcock
//Date written : 10/03/10
//Date compiled : 10/03/10
//Revision history: 1.0
//
//**************************
#include<iostream>
using namespace std;
int main()
{
float x,sum,n,i,t,p;
cout<<"\n Enter your choice:(1) for log series and (2) for exponential series:
";
cin>>p;
if(p==1)
{
cout<<"Enter the number for logarithmic series \n";
cin>>x;
x=x-1;
cout<<"Enter the no of terms in series:";
cin>>n;
sum=0;
t=x;
for(i=1;i<=n;i++)
{
sum+=t/i;
t=t*-1*x;
}
cout<<"\n log(x):"<<sum;
}
if(p==2)
{
cout<<"\n Enter the number for exponential series:";
cin>>x;
cout<<"\n Enter the no of terms:";
cin>>n;
sum=0;
t=1;
for(i=1;i<=n;i++)
{
sum+=t;
t=t*x/i;
}
cout<<"\n The value of the exponential series is "<<sum;
}
return(0);
}

OUTPUT
Enter your choice:(1) for log series and (2) for exponential series:1
Enter the number for logarithmic series
5
Enter the no of terms in series:3
log(x):17.3333

//************************************************************************
// Program Name :Complex.cpp *
// executable file :Complex *
// Author :alfred hitchcock
*
// Date Written :3-02-2010 *
// Date Compiled :3-02-2010 *
// Aim of the Program :To input and display a Complex Number and to *
// find its magnitude and phase angle *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
#include<math.h>
using namespace std;
class complex
{
float Realpart;
float Imgpart;
public:
void getdata();
void showdata();
void magnitude();
void phangle();
};
void complex::getdata()
{
cout<<"Enter Real Part :";
cin>>Realpart;
cout<<"Enter Imaginary Part :";
cin>>Imgpart;
}
void complex::showdata()
{
char s;
if(Imgpart<0)
{
s='-';
cout<<"The Complex Number is :";
cout<<Realpart<<s<<"i"<<-Imgpart<<endl;
}
else
{
s='+';
cout<<"The Complex Number is :";
cout<<Realpart<<s<<"i"<<Imgpart<<endl;
}
}
void complex::magnitude()
{
float magnitude;
magnitude=sqrt((Realpart*Realpart)+(Imgpart*Imgpart));
cout<<"Magnitude is ="<<magnitude<<endl;
}
void complex::phangle()
{
float angle;
angle=atan(Imgpart/Realpart);
float degrees=((180*angle)/3.1412);
cout<<"Phase angle is ="<<degrees<<endl;
}
main()
{
complex c;
c.getdata();
c.showdata();
c.magnitude();
c.phangle();
}

OUTPUT

Enter Real Part :3


Enter Imaginary Part :4
The Complex Number is :3+i4
Magnitude is =5
Phase angle is =53.1367

//************************************************************************
// Program Name :Fraction.cpp *
// executable file :Fraction *
// Author :alfred hitchcock
*
// Date Written :10-02-2010 *
// Date Compiled :10-02-2010 *
// Aim of the Program :To Input,Display and Normalise a Fraction *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
class fraction
{
int numr;
int denr;
public:
void getdata();
void showdata();
void normalise();
};
void fraction::getdata()
{
cout<<"Enter Numerator :";
cin>>numr;
cout<<"Enter Denominator :";
cin>>denr;
}
void fraction::showdata()
{
cout<<"The Fraction is :"<<numr<<"/"<<denr<<endl;
}
main()
{
fraction f;
int gcd(int,int);
f.getdata();
f.normalise();
f.showdata();
}
int gcd(int m,int n)
{
if(n==0)
{
return m;
}
if(n>m)
{
return gcd(n,m);
}
return gcd(n,m%n);
}
void fraction::normalise()
{
int x,y,k;
x=numr;
y=denr;
k=gcd(x,y);
numr=numr/k;
denr=denr/k;
}

OUTPUT
Enter Numerator :3
Enter Denominator :5
The Fraction is :3/5
.......................................................................
//Program name:Transpose of a matrix
//Author name :alfred hitchcock
//Created on :17-03-2010
//Compiled on :17-03-2010
//Objective :To transpose a matrix
#include<iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],i,j,r,c,t;
cout<<"Enter the rows and coloumns"<<endl;
cin>>r>>c;
cout<<"Enter the matrix\n";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<a[i][j]<<"\t";
cout<<endl;
}
for(i=0;i<r;++i)
for(j=0;j<c;j++)
{
b[j][i]=a[i][j];
}
cout<<"Transpose"<<endl;
for(i=0;i<r;++i)
{
for(j=0;j<c;j++)
cout<<b[i][j]<<"\t";
cout<<"\n";
}
return 0;
}

OUTPUT

Enter the rows and coloumns


2 2
Enter the matrix
1 2
4 5
1 2
4 5
Transpose
1 4
2 5

//************************************************************************
// Program Name :complex_constructors.cpp *
// executable file :complex_constructors *
// Author :alfred hitchcock
*
// Date Written :3-02-2010 *
// Date Compiled :3-02-2010 *
// Aim of the Program :To pass default arguments to a class of *
// complex nos. using constructors *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
class complex
{
int real;
int imag;
public:
complex()
{
real=0;
imag=0;
}
complex(int a)
{
real=a;
imag=0;
}
complex(int a,int b)
{
real=a;
imag=b;
}
complex(complex &obj)
{
real=obj.real;
imag=obj.imag;
}
void display();
};
void complex::display()
{
cout<<"The Complex Number is :"<<real<<"+i"<<imag<<endl;
}
int main()
{
complex O1;
O1.display();
complex O2(2);
O2.display();
complex O3(3,4);
O3.display();
complex O4(O3);
O4.display();
}

OUTPUT
The Complex Number is :0+i0
The Complex Number is :2+i0
The Complex Number is :3+i4
The Complex Number is :3+i4

//************************************************************************
// Program Name :stu_database.cpp *
// executable file :stu_database *
// Author :alfred hitchcock
*
// Date Written :17-03-2010 *
// Date Compiled :17-03-2010 *
// Aim of the Program :To create a student database which *
// stores his *
// Roll no. Name,Semester,Branch and GPA *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
#include<string.h>
class student
{
int rollno;
char name[25];
int sem;
char branch;
float GPA;
public:
void indata();
void outdata();
friend void search(student[],int);
};
void student::indata()
{
cout<<"Enter roll no :";
cin>>rollno;
cout<<"Enter Name (max 24 char) :";
cin>>name;
cout<<"Enter Semester :";
cin>>sem;
cout<<"Branch (T/A/E/M/N/C/R) ";
cin>>branch;
cout<<"Enter GPA :";
cin>>GPA;
}
void student::outdata()
{
cout<<"Roll No: "<<rollno;
cout<<" \n Name :";
cout<<name;
cout<<" \n Semester :"<<sem;
cout<<" \n Branch :"<<branch;
cout<<" \n GPA :"<<GPA<<endl;
}
void search (student a[],int size)
{
int x,flag=0;
cout<<" Enter Roll No: to be searched :";
cin>>x;
for(int i=0;i<size;i++)
{
if(a[i].rollno==x)
{
cout<<"Record Found!!! \n";
flag=1;
a[i].outdata();
break;
}
}
if(flag==0)
{
cout<<"Not Found!!! \n";
}
}
main()
{
student s[25];
int size,i;
cout<<"Enter No. of records :";
cin>>size;
for(i=0;i<size;i++)
{
s[i].indata();
}
for(i=0;i<size;i++)
{
s[i].outdata();
}
search(s,size);
}

OUTPUT
Enter No. of records :1
Enter roll no :20
Enter Name (max 24 char) :james
Enter Semester :6
Branch (T/A/E/M/N/C/R) e
Enter GPA :8
Roll No: 20
Name :james
Semester :6
Branch :e
GPA :8
Enter Roll No: to be searched :20
Record Found!!!
Roll No: 20
Name :james
Semester :6
Branch :e
GPA :8

//************************************************************************
// Program Name :GPA_modify.cpp *
// executable file :GPA_modify *
// Author :alfred hitchcock
*
// Date Written :24-03-2010 *
// Date Compiled :24-03-2010 *
// Aim of the Program :To modify the GPA after it has been
entered
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
#include<string.h>
class student
{
int rollno;
char name[25];
int sem;
char branch;
float GPA;
public:
void indata();
void outdata();
friend void modify(student[],int,int);
};
void student::indata()
{
cout<<"Enter roll no :";
cin>>rollno;
cout<<"Enter Name (max 24 char) :";
cin>>name;
cout<<"Enter Semester :";
cin>>sem;
cout<<"Branch (T/A/E/M/N/C/R) ";
cin>>branch;
cout<<"Enter GPA :";
cin>>GPA;
}
void student::outdata()
{
cout<<"Roll No: "<<rollno;
cout<<" \n Name :";
cout<<name;
cout<<" \n Semester :"<<sem;
cout<<" \n Branch :"<<branch;
cout<<" \n GPA :"<<GPA<<endl;
}
void modify(student a[],int size,int rollno)
{
float GPA;
int flag=0;
cout<<"Enter new GPA :";
cin>>GPA;
for(int i=0;i<size;i++)
{
if(a[i].rollno==rollno)
{
a[i].GPA=GPA;
flag=1;
}
}
if(flag==0)
{
cout<<" Element not found!!!";
}
}
main()
{
student s[50];
int size,rollno,i;
cout<<"Enter the No. of Records :";
cin>>size;
for(i=0;i<size;i++)
{
s[i].indata();
}
cout<<"Enter Roll no. to be Modified ";
cin>>rollno;
modify(s,size,rollno);
for(i=0;i<size;i++)
{
s[i].outdata();
}
}

OUTPUT

Enter the No. of Records :3


Enter roll no :21
Enter Name (max 24 char) :james
Enter Semester :3
Branch (T/A/E/M/N/C/R) e
Enter GPA :8
Enter roll no :22
Enter Name (max 24 char) :john
Enter Semester :3
Branch (T/A/E/M/N/C/R) e
Enter GPA :8.1
Enter roll no :23
Enter Name (max 24 char) :johnson
Enter Semester :3
Branch (T/A/E/M/N/C/R) e
Enter GPA :7,8
Enter Roll no. to be Modified 22
Enter new GPA : 8.2
Roll No: 21
Name :james
Semester :3
Branch :e
GPA :8
Roll No: 22
Name :john
Semester :3
Branch :e
GPA :8.2
Roll No: 23
Name :johnson
Semester :3
Branch :e
GPA :7

//************************************************************************
// Program Name :Time.cpp *
// executable file :Time *
// Author :alfred hitchcock
*
// Date Written :17-03-2010 *
// Date Compiled :17-03-2010 *
// Aim of the Program :Increment and addition of two times *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
class time
{
private:
int hour;
int minute;
int second;
public:
void input();
void display();
void operator++();
time operator+(time);
};
void time::input()
{
cout<<"\nEnter time in hr,min and sec (in 12 hr base):";
cin>>hour>>minute>>second;
}
void time::display()
{
cout<<hour<<":"<<minute<<":"<<second;
}
void time::operator++()
{
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour>12)
hour=1;
}
}
cout<<"\nTime in next second is:";
display();
}
class time time::operator+(time t)
{
time temp;
temp.second=second+t.second;
temp.minute=minute+t.minute;
temp.hour=hour+t.hour;
if(temp.second>=60)
{
temp.minute+=temp.second/60;
temp.second=temp.second%60;
}
if(temp.minute>=60)
{
temp.hour+=temp.minute/60;
temp.minute=temp.minute%60;
}
return (temp);
}
int main()
{
class time t1,t2,t3,t4;
cout<<"\nEnter the time you want to increment";
t1.input();
cout<<"\nTIME T1 is ";
t1.display();
cout<<"\nGoing to increment T1";
++t1;
cout<<"\nEnter time you want to add";
t2.input();
t3.input();
cout<<"\nTIME T2 is ";
t2.display();
cout<<"\nTIME T3 is ";
t3.display();
cout<<"\nGoing to find T2+T3";
t4=t2+t3;
t4=t4+t1;
cout<<"\nT2+T3=";
t4.display();
return (0);
}

OUTPUT
Enter the time you want to increment
Enter time in hr,min and sec (in 12 hr base):5 40 23
TIME T1 is 5:40:23
Going to increment T1
Time in next second is:5:40:24
Enter time you want to add
Enter time in hr,min and sec (in 12 hr base):1 20 20
Enter time in hr,min and sec (in 12 hr base):2 5 4
TIME T2 is 1:20:20
TIME T3 is 2:5:4
Going to find T2+T3
T2+T3=9:5:48a

//************************************************************************
// Program Name :timeconversion.cpp *
// executable file :timeconversion *
// Author :alfred hitchcock
*
// Date Written :17-03-2010 *
// Date Compiled :17-03-2010 *
// Aim of the Program :Increment and addition of two times *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
class time12
{
int hr,min,sec;
char choice[2];
public:
void input();
void display();
void convert();
void hour(int,int,int);
};
class time24
{
int hr,min,sec;
char choice[2];
public:
void input();
void display();
void convert();
void hour(int,int,int);
};
void time12::input()
{
cout<<"Enter time in 12 hour mode as hh:mm:ss am/pm \n";
cin>>hr>>min>>sec>>choice[0]>>choice[1];
}
void time12::display()
{
cout<<hr<<":"<<min<<":"<<sec<<" "<<choice[0]<<choice[1]<<"\n";
}
void time12::convert()
{
time24 temp;
if(choice[0]=='p' && choice[1]=='m')
temp.hour((hr+12),min,sec);
else
temp.hour(hr,min,sec);
cout<<"Time in 24 hour base is :";
temp.display();
}
void time12::hour(int h,int m,int s)
{
if(h>12)
{
hr=h-12;
choice[0]='p';
choice[1]='m';
}
else
{
hr=h;
choice[0]='a';
choice[1]='m';
}
min=m;
sec=s;
}
void time24::input()
{
cout<<"Enter time in 24 hour mode\n";
cin>>hr>>min>>sec;
}
void time24::display()
{
cout<<hr<<":"<<min<<":"<<sec<<"\n";
}
void time24::convert()
{
time12 temp;
temp.hour(hr,min,sec);
cout<<"Time in 12 hour base is :";
temp.display();
}
void time24::hour(int h,int m,int s)
{
hr=h;
min=m;
sec=s;
}
int main()
{
time12 O1;
time24 O2;
O1.input();
cout<<"Time in 12 hour base:";
O1.display();
O1.convert();
O2.input();
cout<<"Time in 24 hour base is :";
O2.display();
O2.convert();
}

OUTPUT

Enter time in 12 hour mode as hh:mm:ss am/pm


5 40 20 pm
Time in 12 hour base:5:40:20 pm
Time in 24 hour base is :17:40:20
Enter time in 24 hour mode
16 20 3
Time in 24 hour base is :16:20:3
Time in 12 hour base is :4:20:3 pm

......................................................................

//Program Name : empdatabase.cpp


//Author Name : alfred hitchcock
//Date written : 10-02-2010
//Date compiled : 10-02-2010
//Revision History : 1.0
#include<iostream>
using namespace std;
class employee
{
private:
int employeeno;
char employeename[30], designation[30];
protected:
float basicpay;
public:
employee()
{
cout<<"\nEnter employee no : ";
cin>>employeeno;
cout<<"Enter employee name : ";
cin>>employeename;
cout<<"Enter designation : ";
cin>>designation;
cout<<"Enter basic pay : ";
cin>>basicpay;
}
void display();
};
class executive:public employee
{
private:
float allowance;
float salary;
public:
executive()
{
cout<<"\nEnter allowance : ";
cin>>allowance;
}
void total();
void display();
};
void employee::display()
{
cout<<"\nEmployee no : "<<employeeno;
cout<<"\nEmployee name : "<<employeename;
cout<<"\nDesignation : "<<designation;
cout<<"\nBasic pay : "<<basicpay;
}
void executive::total()
{
salary=basicpay + allowance;
}
void executive::display()
{
cout<<"\nAllowance : Rs. "<<allowance;
cout<<"\nSalary : Rs. "<<salary;
}
int main()
{
executive E;
E.total();
E.employee::display();
E.display();
return 0;
}

OUTPUT
Enter employee no : 23
Enter employee name : john
Enter designation : ceo
Enter basic pay : 12000
Enter allowance : 10000
Employee no : 23
Employee name : john
Designation : ceo
Basic pay : 12000
Allowance : Rs. 10000
Salary : Rs. 22000

//**********************************************
//Program name : String manipulation
//Date written : 27-03-2010
//Date compilde: 27-03-2010
//Author name : alfred hitchcock
//Revision history : 1.0
//Version :1.0
//**********************************************
#include<iostream>
using namespace std;
class string1
{
char sour[20],dest[20],sub[20];
public:
void count();
void compare();
void copy();
void substring();
};
void string1::count()
{
int i;
cout<<"\nEnter string:\n";
cin>>sour;
for(i=0;sour[i]!='\0';i++);
cout<<"\nThe length of the string is "<<i;
}
void string1::compare()
{
int i;
cout<<"Enter the first string:\n";
cin>>sour;
cout<<"\nEnter the second string:\n";
cin>>dest;
int f1=0;
for(i=0;sour[i]!='\0'||dest[i]!='\0';i++)
if (sour[i]!=dest[i])
{
cout<<"\nThe strings are different\n";
f1=1;
break;
}
if(f1==0)
cout<<"\nThe strings are same\n";
}
void string1::copy()
{
int i;
cout<<"\nEnter the string:";
cin>>sour;
for(i=0;sour[i]!='\0';i++)
dest[i]=sour[i];
dest[i]=sour[i];
cout<<"\nThe string to be copied : "<<sour;
cout<<"\nThe copied string : "<<dest;
}
void string1::substring()
{
int slen,llen,temp,f=0,i,j;
cout<<"\nEnter the main string:\n";
cin>>sour;
cout<<"\nEnter the substring:\n";
cin>>dest;
for(i=0;sour[i]!='\0';i++);
llen=i;
for(i=0;dest[i]!='\0';i++);
slen=i;
for(i=0;i<=(llen-slen);i++)
{
temp=i;
for(j=0;j<slen;j++,temp++)
{
if(sour[temp]!=dest[j])
{
f=1;
}
}
}
if(j==slen)
cout<<"\nThe position of substring is: "<<i-1;
else
cout<<"\nThe substring is not found\n";
}
int main()
{
string1 s;
xy:cout<<"\n\tMENU\n1.Length of string\n2.Compare two strings\n3.Copy string\n4
.Find substring in a string\nEnter your choice:\n";
int c;
cin>>c;
switch(c)
{
case 1:
s.count();
break;
case 2:
s.compare();
break;
case 3:
s.copy();
break;
case 4:
s.substring();
break;
default:
cout<<"\nSorry!!!Wrong choice\n";
}
char ch;
cout<<"\n\nDo you wish to continue:(y/n): ";
cin>>ch;
if(ch=='y')
goto xy;
return 0;
}

OUTPUT

MENU
1.Length of string
2.Compare two strings
3.Copy string
4.Find substring in a string
Enter your choice:
4
Enter the main string:
indiaism
Enter the substring:
sm
The position of substring is: 6

//******************************************************************************
***
//Program Name : euler.cpp
//Author : alfred hitchcock
//Date written : 10-02-2010
//Date Compiled : 10-02-2010
//Aim of the program : To solve the differential equation using euler's method
//******************************************************************************
***
#include<iostream>
using namespace std;
#include<math.h>
float df(float x,float y)
{
return ((1+x)*y*y)/2;
}
int main()
{
float x0,y0,h,x,x1,y1;
cout<<"\nEnter the values of x0,y0,h,x"<<endl;
cin>>x0>>y0>>h>>x;
x1=x0;
y1=y0;
while(1)
{
if(x1>x)
return 0;
y1+=h*df(x1,y1);
x1+=h;
cout<<"\nWhen x = "<<x1<<"\ty = "<<y1<<endl;
}
}

OUTPUT
Enter the values of x0,y0,h,x
0 1 .1 .02
When x = 0.1 y = 1.05

................................................................
//Program Name :rungakutta.cpp
//Author :alfred hitchcock
//Date written :27-03-2010
//Date compiled :27-03-2010

#include<iostream>
#include<math.h>
using namespace std;
float f( float x, float y)
{
return ((1+x)*y*y)/2;
}
int main()
{
float x, x0, y0, h, xn, y, k1,k2,k3,k4,k;
cout<<"\nEnter the values of x0, y0, h, xn :\n";
cin>>x0>>y0>>h>>xn;
x=x0;
y=y0;
while(1)
{
if(x==xn)
break;
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x+=h;
y+=k;
}
cout<<"\nWhen x="<<x<<" y="<<y<<"\n";
return 0;
}

OUTPUT
Enter the values of x0, y0, h, xn :
0 1 0.2 0.2
When x=0.2 y=1.1236

//***********************************************************************
//Program Name : elimination.cpp *
//Author : alfred hitchcock
*
//Date written : 17-02-2010 *
//Date Compiled : 17-02-2010 *
//Aim of the program : To solve the equation by Gauss Elimination method*
//***********************************************************************
#include<iostream>
using namespace std;
int main()
{
float a[20][20],ratio,x[20];
int i,j,k,n;
cout<<"\nEnter the order of the matrix";
cin>>n;
cout<<"\nEnter the coefficients and RHS";
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
cin>>a[i][j];
cout<<"\n";
}
}
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(i!=j)
{
ratio=a[i][j]/a[j][j];
for(k=0;k<n+1;k++)
a[i][k]=a[i][k]-ratio*a[j][k];
}
}
}
for(i=n-1;i>=0;i--)
{
int s=0;
for(j=i+1;j<n;j++)
s+=a[i][j]*x[j];
x[i]=(a[i][n]-s)/a[i][i];
}
cout<<"\nThe solution is";
for(i=0;i<n;i++)
cout<<"\n"<<"x("<<i+1<<") ="<<x[i]<<endl;
return 0;
}

OUTPUT
Enter the order of the matrix 2 2
Enter the coefficients and RHS
2 3 5
3 4 6

The solution is
x(1) =-0.25
x(2) =1.75

//**************************************************************************
// Program Name :gaussjordan.cpp *
// Author :alfred hitchcock
*
// Date Written :17-02-2010 *
// Date Compiled :17-02-2010 *
// Aim of the Program :Solution of linear eqs(Gauss-Jordan method) *
//**************************************************************************
#include<iostream>
using namespace std;
main()
{
float a[20][20],ratio;
int i,j,k,n;
cout<<"\nEnter the order of the matrix";
cin>>n;
cout<<"\nEnter the coefficients and RHS";
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
cin>>a[i][j];
cout<<"\n";
}
}
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
if(i!=j)
{
ratio=a[i][j]/a[j][j];
for(k=0;k<n+1;k++)
a[i][k]=a[i][k]-a[j][k]*ratio;
}
}
}
cout<<"\nThe solution is";
for(i=0;i<n;i++)
cout<<"\n"<<"x("<<i+1<<") ="<<a[i][n]/a[i][i]<<endl;
}

OUTPUT

Enter the order of the matrix 3 3


Enter the coefficients and RHS
2 3 4 5
5 6 7 5
1 3 7 9
The solution is
x(1) =-5.5
x(2) =6
x(3) =-0.5

//***************************************************************
//Program Name :gaussiedal.cpp *
//Author :alfred hitchcock *
//Date written :27/03/10 *
//Date compiled :27/03/10 *
//Aim of Program:simulataneous eq soln using gauss siedal method*
//***************************************************************
#include<iostream>
#include<iomanip.h>
#include<math.h>
#define n 2
int main()
{
float a[n][n+1],x[n],aerr,maxerr,t,s,err;
int i,j,itr,maxitr;
//initializing array
for(i=0;i<n;i++)
x[i]=0;
cout<<"enter elements of aug mat";
for(i=0;i<n;i++)
for(j=0;j<n+1;j++)
cin>>a[i][j];
cout<<"enter allowed error, max iterartons"<<endl;
cin>>aerr>>maxitr;
cout<<"iteration "<<"\t"<<"x[1]"<<"\t"<<"x[2]"<<"\t"<<"x[3]"<<endl;
for(itr=1;itr<=maxitr;itr++)
{
maxerr=0;
for(i=0;i<n;i++)
{
s=0;
for(j=0;j<n;j++)
if(j!=i)
s+=a[i][j]*x[j];
t=(a[i][n]-s)/a[i][i];
err=fabs(x[i]-t);
if(err>maxerr)
maxerr=err;
x[i]=t;
}
cout<<"\t"<<itr;
for(i=0;i<n;i++)
cout<<"\t"<<setprecision(4)<<x[i];
cout<<endl;
if(maxerr<aerr)
{
cout<<"converges in "<<setw(3)<<itr<<"iterartions"<<endl;
for(i=0;i<n;i++)
cout<<"x["<<setw(3)<<(i+1)<<"]"<<"\t"<<setprecision(4)<<x[i]<<endl;
return 0;
}
}
cout<<"soln does not converge,"<<"iterations not sufficient"<<endl;
return 1;
}
OUTPUT

enter elements of aug mat 2 3 5


1 4 7
enter allowed error, max iterartons
0.0001 15
iteration x[1] x[2] x[3]
1 2.5 1.125
2 0.8125 1.547 
3 0.1797 1.705
4 -0.05762 1.764
5 -0.1466 1.787
6 -0.18 1.795
7 -0.1925 1.798
8 -0.1972 1.799
9 -0.1989 1.8
10 -0.1996 1.8
11 -0.1999 1.8
12 -0.1999 1.8
converges in 12iterartions
x[ 1] -0.1999
x[ 2] 1.8

//*************************************************************************
//Program Name : trapezoidal.cpp
*
//Author : alfred hitchcock
*
//Date written : 17-03-2010
//Date Compiled : 17-03-2010
*
//Aim of the program : to find integral of the eq'n
*
//******************************************************************************
**
#include<iostream>
using namespace std;
#include<math.h>
float y(float x)
{
return (x*x);
}
main()
{
float x0,xn,h,s;
int i,n;
cout<<"\nEnter the values of x0,xn,no of sub-intervals"<<endl;
cin>>x0>>xn>>n;
h=(xn-x0)/n;
s=y(x0)+y(xn);
for(i=1;i<=n-1;i++)
s+=2*y(x0+i*h);
cout<<"\nValue of integral is "<<(h/2)*s<<endl;
}

OUTPUT

Enter the values of x0,xn,no of sub-intervals


1 7 3
Value of integral is 118

//*************************************************************************
//Program Name : simpson.cpp
*
//Author : alfred hitchcock
*
//Date written : 27-03-2010
*
//Date Compiled : 27-03-2010
*
// Aim of the program : To find integral of the eq'n x*x
*
//******************************************************************************
********
#include<iostream>
using namespace std;
#include<math.h>
float y(float x)
{
return (x*x);
}
main()
{
float x0,xn,h,s;
int i,n;
cout<<"\nEnter the values of x0,xn,no of sub-intervals"<<endl;
cin>>x0>>xn>>n;
h=(xn-x0)/n;
s=y(x0)+y(xn)+4*y(x0+h);
for(i=3;i<=n-1;i+=2)
s+=4*y(x0+i*h)+2*y(x0+(i-1)*h);
cout<<"\nValue of integral is "<<(h/3)*s<<endl;
}

OUTPUT

Enter the values of x0,xn,no of sub-intervals


1 7 3
Value of integral is 57.3333

//******************************************************************************
**
//Program name :newtonraphs.cpp
*
//Author :alfred hitchcock
*
//Date Written :17-03-2010
*
//Date Compiled :17-03-2010
*
//Aim of program :To find root of an equation using newton-raphson method
*
//******************************************************************************
**
#include<iostream>
using namespace std;
#include<iomanip.h>
#include<math.h>
float f(float x)
{
return (x*x+3*x+1);
}
float df(float x)
{
return (2*x+3);
}
int main()
{
int p,maxt;
float h,x0,x1,aerr;
cout<<"\nEnter the values of x0,allowed error,maximum iterations"<<endl;
cin>>x0>>aerr>>maxt;
for(p=1;p<=maxt;p++)
{
h=f(x0)/df(x0);
x1=x0-h;
cout<<"Iteration no."<<p<<"x="<<setw(9)<<setprecision(6)<<x1<<endl;
if(fabs(h)<aerr)
{
cout<<"\nAfter"<<p<<"iterations,root"<<"="<<x1<<endl;
return 0;
}
x0=x1;
}
cout<<"\nSolutions does not converge"<<",iterations not sufficient"<<end
l;
return 1;
}

OUTPUT

Enter the values of x0,allowed error,maximum iterations


1 0.0001 15
Iteration no.1x= 0
Iteration no.2x=-0.333333
Iteration no.3x=-0.380952
Iteration no.4x=-0.381966
Iteration no.5x=-0.381966
After 5 iterations,root=-0.381966

/******************************************************************
*****
// Program Name :Functionoverload.cpp *
// executable file :Functionoverload *
// Author :alfred hitchcock
*
// Date Written :17-02-2010 *
// Date Compiled :17-02-2010 *
// Aim of the Program :Concept of Function over Loading *
// Version :1.0 *
//***********************************************************************
//***********************************************************************
#include<iostream>
using namespace std;
#include<math.h>
void area(double a,double b,double c)
{
double s=(a+b+c)/2;
cout<<s;
double area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"\nArea of the triangle is:"<<area<<" square units";
}
void area(float r,float pi)
{
float area=pi*r*r;
cout<<"\nArea of the circle is:"<<area<<" square units";
}
void area(int a)
{
float area=a*a;
cout<<"\nArea of the square is:"<<area<<" square units";
}
void area(int a,int b)
{
float area=a*b;
cout<<"\nArea of the rectangle is:"<<area<<" square units";
}
int main()
{
cout<<"\nThis program shows the concept of function overloading";
cout<<"\nEnter the three sides of a triangle:";
double A,B,C;
cin>>A>>B>>C;
area(A,B,C);
cout<<"\nEnter the radius of circle:";
float r;
cin>>r;
area(r,3.14);
cout<<"\nEnter the side of a square:";
int a,b;
cin>>a;
area(a);
cout<<"\nEnter two sides of a rectangle:";
cin>>a>>b;
area(a,b);
return (0);
}

OUTPUT
This program shows the concept of function overloading
Enter the three sides of a triangle:2 3 4
4.5
Area of the triangle is:2.90474 square units
Enter the radius of circle:3
Area of the circle is:28.26 square units
Enter the side of a square:4
Area of the square is:16 square units
Enter two sides of a rectangle:5
4
Area of the rectangle is:20 square units

//************************************************************************
// Program Name :Date.cpp *
// executable file :Date *
// Author :alfred hitchcock
*
// Date Written :3-02-2010 *
// Date Compiled :3-02-2010 *
// Aim of the Program :Comparison of Dates (using constructors) *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
class date
{
private:
int dd;
int mm;
int yy;
public:
date()
{
cout<<"\nYou are in default constructor";
}
void input();
void compare(date);
};
void date::input()
{
cout<<"\nEnter date in dd:mm:yy format:";
cin>>dd>>mm>>yy;
}
void date::compare(date d)
{
if(dd==d.dd)
if(mm==d.mm)
if(yy==d.yy)
cout<<"\nDates are Same";
else
cout<<"\nDates are different";
else
cout<<"\nDates are different";
else
cout<<"\nDates are different";
}
int main()
{
date d1,d2;
d1.input();
d2.input();
d2.compare(d1);
return (0);
}

OUTPUT
You are in default constructor
Enter date in dd:mm:yy format:25 03 2010
Enter date in dd:mm:yy format:26 03 2010
Dates are different

//************************************************************************
// Program Name :Hospital.cpp *
// executable file :Hospital *
// Author :alfred hitchcock
*
// Date Written :24-03-2010 *
// Date Compiled :24-03-2010 *
// Aim of the Program :Create a Hospital Data base *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
using namespace std;
#include<string.h>
class date
{
private:
int dd;
int mm;
int yy;
public:
void input();
void display();
};
class basepatient
{
private:
char name[30];
date doa,dod;
public:
char disease[30];
void input();
void display();
};
class derivedpatient:public basepatient
{
public:
int age;
void input();
friend void display(derivedpatient,int);
};
void date::input()
{
cin>>dd>>mm>>yy;
}
void date::display()
{
cout<<dd<<":"<<mm<<":"<<yy;
}
void basepatient::input()
{
cout<<"\nEnter Name of Patient:";
cin>>name;
cout<<"Enter disease:";
cin>>disease;
cout<<"Enter admission date in dd:mm:yy format:";
doa.input();
cout<<"Enter dicharge date in dd:mm:yy format:";
dod.input();
}
void basepatient::display()
{
cout<<"\nName:";
cout<<name;
cout<<"\nAdm.Date:";
doa.display();
cout<<"\nDisc.Date";
dod.display();
cout<<endl;
}
void derivedpatient::input()
{
basepatient::input();
cout<<"\nEnter Age:";
cin>>age;
}
void display(derivedpatient *p,int n)
{
cout<<"\nEnter Age and disease:";
char idisease[30];
int iage;
cin>>iage;
cin>>idisease;
int cmp,flag=0;
cout<<"\nChecking for details that match the given details:";
cout<<"\nAge:"<<iage;
cout<<"\nDisease:";
cout<<idisease;
for(int i=0;i<n;i++)
{
cmp=strcmp(idisease,(p+i)->disease);
if(iage==(p+i)->age&&cmp==0)
{
(p+i)->basepatient::display();
flag++;
}
}
if(flag==0)
cout<<"\nNot found";
}
int main()
{
derivedpatient A[10];
int N;
cout<<"Enter number of details you want to enter:";
cin>>N;
for(int i=0;i<N;i++)
A[i].input();
display(A,N);
return (0);
}

OUTPUT

Enter number of details you want to enter:1


Enter Name of Patient:john
Enter disease:cholera
Enter admission date in dd:mm:yy format:20 3 2008
Enter dicharge date in dd:mm:yy format:27 3 2008
Enter Age:30
Enter Age and disease:30 cholera
Checking for details that match the given details:
Age:30
Disease:cholera
Name:john
Adm.Date:20:3:2008
Disc.Date27:3:2008

.........................................................................

//Area of circle, triagle, rectangle


//program name : Area.cpp
//author name : alfred hitchcock
//Date written : 27-01-2010
//Date compiled : 27-01-2010
//Revision History
// 1.o
#include<iostream>
#include<cmath>
using namespace std;
main()
{
float area,s,x,y,z;
int choice,check;
do
{
cout<<"Enter the choice : 1.RECTANGLE 2.CIRCLE 3.TRIANGLE \n";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter the length and breadth of Rectangle\n";
cin>>x>>y;
area=(x*y);
cout<<"\t AREA OF RECTANGLE = "<<area;
break;
case 2:
cout<<"\n Enter the radius of circle : ";
cin>>x;
area=(3.14*x*x);
cout<<"\n\t AREA OF CIRCLE : "<<area;
break;
case 3:
cout<<"\n Enter the sides of triangle\n";
cin>>x>>y>>z;
s=((x+y+z)/2);
area=sqrt(s*(s-x)*(s-y)*(s-z));
cout<<"\t AREA OF TRIANGLE= "<<area;
break;
default:
cout<<"INVALID CHOICE...TRY AGAIN";
break;
}
cout<<"\n Do you want to continue?...1.CONTINUE 2.QUIT\n";
cin>>check;
}while(check==1);
}

OUTPUT

Enter the choice : 1.RECTANGLE


2.CIRCLE
3.TRIANGLE
1

Enter the length and breadth of Rectangle


20 10
AREA OF RECTANGLE = 200
Do you want to continue?...1.CONTINUE 2.QUIT
//************************************************************************
// Program Name :Classmembers.cpp *
// executable file :Classmembers *
// Author :alfred hitchcock
*
// Date Written :3-02-2010 *
// Date Compiled :3-02-2010 *
// Aim of the Program :Define a class along with member functions *
// Version :1.0 *
//************************************************************************
//************************************************************************
#include<iostream>
#include<math.h>
using namespace std;
class triangle
{
protected:
double a,b,c;
double area;
public:
void input();
void AREA();
};
class dtriangle:public triangle
{
public:
void display();
};
void triangle::input()
{
cout<<"\nEnter three sides of triangle";
cin>>a>>b>>c;
}
void triangle::AREA()
{
area=(a+b+c)/2;
area=sqrt(area*(area-a)*(area-b)*(area-c));
}
void dtriangle::display()
{
cout<<"\nThe three sides are "<<a<<","<<b<<","<<c;
AREA();
cout<<"\nArea="<<area<<endl;
}
int main()
{
dtriangle T;
T.input();
T.display();
return (0);
}
OUTPUT

Enter three sides of triangle 4 3 5


The three sides are 4,3,5
Area=6

//**************************************************************************
// Program Name :Line.cpp *
// executable file :Line *
// Author :alfred hitchcock
*
// Date Written :27-03-2010 *
// Date Compiled :27-03-2010 *
// Aim of the Program :Define a class line with co ordinates *
// Version :1.0 *
//**************************************************************************
//**************************************************************************
#include<iostream>
using namespace std;
#include<math.h>
class line
{
double x1,y1,x2,y2;
public:
line()
{
cout<<"\nEnter the endpoint as x & y cooornates";
cin>>x1>>y1>>x2>>y2;
}
friend class lines;
};
class lines
{
double m1,m2,angle;
line l1,l2;
public:
void calculate()
{
m1=(l1.y2-l1.y1)/(l1.x2-l1.x1);
m2=(l2.y2-l2.y1)/(l2.x2-l2.x1);
angle=atan(m1*m2/(m1-m2));
}
void display()
{
cout<<"\nThe slope of first line : "<<m1;
cout<<"\nThe slope of second line : "<<m2;
cout<<"\nThe angle between lines : "<<angle;
}
};
main()
{
lines a;
a.calculate();
a.display();
}

OUTPUT

Enter the endpoint as x & y cooornates 2 3


5 7
Enter the endpoint as x & y cooornates 3 4
8 9
The slope of first line : 1.33333
The slope of second line : 1
The angle between lines : 1.32582

You might also like