Program 6 & 7
Program 6 & 7
Experiment No: 6
Write a C++ program to create a class with data members principle, time and rate. Create
a member functions to accept data values to compute simple interest and to display the
result.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class interest
{
private:
double p,t,r,si;
public:
void getdata( );
void compute( );
void display( );
};
void interest::getdata( )
{
cout<<"Input P, T, R";
cin>>p>>t>> r;
}
void interest::compute( )
{
si = (p*t*r) / 100;
}
void interest::display( )
{
cout<<"Principle is "<<p<<endl;
cout<<"Time is " <<t<<endl;
cout<<"Rate is "<<r<<endl;
cout<<"Simple interest is "<<si<<endl;
getch( );
}
int main( )
{
interest I;
clrscr( );
I.getdata( );
I.compute( );
I.display( );
return 0;
}
-23-
Experiment No: 7
Write a C++ program to create a class with data members a,b,c and member functions to
input data, compute the discriminant based on the following conditions and print the
roots.
a) If discriminant = 0, print the roots that are equal
b) If the discriminant is > 0, print the real roots
c) If the discriminant< 0, print that the roots are imaginary and exit the program.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
class quad
{
private:
double a,b,c,r1,r2,d;
public:
void getdata( );
void roots( );
void display( );
};
void quad::getdata( )
{
cout<<"Enter the co-efficients:" ;
cin>>a>>b>>c;
}
void quad::roots( )
{
d = b*b-4*a*c;
if(d == 0)
{
cout<<"Roots are equal"<<endl;
r1 = (-b-sqrt(d)) /(2*a);
r2 = r1;
}
else
if(d > 0)
{
cout<<"Roots are positive and different:"<<endl;
r1 = (-b+sqrt(d)) / (2*a);
r2 = (-b-sqrt(d)) / (2*a);
}
-24-
else
{
cout<<"Roots are imaginary"<<endl;
exit(0);
}
void quad::display( )
{
cout<<"First root is = "<<r1<<endl;
cout<<"Second root is = "<<r2;
}
void main( )
{
quad Q;
clrscr( );
Q.getdata( );
Q.roots( );
Q.display( );
getch( );
}