Write a C++ program using class to calculate simple interest amount use default value for rate.
#include<iostream.h>
#include<conio.h>
class interest
{
float p;
int n;
float r;
public:
void accept(float a,int b,float c=0.10)
{
p=a;
n=b;
r=c;
}
void display()
{
cout<<"\n\nP = "<<p<<" N = "<<n<<" R = "<<r;
}
float value();
};
float interest::value()
{
float A;
A=(p*n*r)/100;
return A;
}
int main()
{
interest i;
clrscr();
i.accept(5000.00,2,0.50);
i.display();
cout<<"\n\nThe Simple Interest Amount is : ";
cout<<i.value();
i.accept(5000.00,2);
i.display();
cout<<"\n\nThe Simple Interest Amount is : ";
cout<<i.value();
getch();
return 0;
}
Sourse Codes For You
Tuesday, 7 July 2015
C++ program using class to calculate simple interest
C++ Program - Queue Operations Using Class
C++ PROGRAM TO IMPLEMENT QUEUE USING ARRAY.
#include<iostream.h>
#include<conio.h>
class queue
{
public:
int q[5],front,rear,x,result;
void enq();
void dque();
void disp();
queue()
{
front=0;
rear=0;
}
};
void queue::enq()
{
if(rear>=5)
cout<<"\nQueue overflow!!\n";
else
{
cout<<"\nEnter the number to be inserted: ";
cin>>x;
rear++;
q[rear]=x;
cout<<"\nNumber pushed in the queue:"<<q[rear];
}
}
void queue::dque()
{
if(rear==0)
cout<<"\nQueue underflow!!\n";
else
{
if(front==rear)
{
front=0;
rear=0;
}
else
front++;
}
cout<<"\nDeleted element is:";
result=q[front];
cout<<result;
}
void queue::disp()
{
if(rear==0)
cout<<"\nQueue underflow!!\n";
else
cout<<"\nContents of queue is:";
for(int i=front+1;i<=rear;i++)
cout<<q[i]<<"\t";
}
void main()
{
int c;
queue qu;
clrscr();
// cout<<"\n*****";
// cout<<"\nQUEUE";
// cout<<"\n*****";
do
{
cout<<"\n1.Insertion\n2.Deletion\n3.Display\n";
cout<<"\nEnter your choice:";
cin>>c;
switch(c)
{
case 1:
qu.enq();
break;
case 2:
qu.dque();
break;
case 3:
qu.disp();
break;
default:
cout<<"\nInvalid choice!!\n";
}
}
while(c<4);
getch();
}
OUTPUT
C++ Program - Implementation of Stack
This C++ program implements the following stack operations.
Push
Pop
Top
Empty
STEPS:
Ø Create an array to store the stack elements.
Ø Get the size of the stack.
Ø To push an element into the stack check if the top element is less than the size and increment
the top.
Ø Else print overflow .
Ø To pop an element, check if the stack is empty and decrement the stack.
Ø If all the elements are popped, print underflow.
Ø Find the topmost element in the stack by checking if the size is equal to top.
Ø If the stack is empty print empty, else print not empty.
CODING:
#include<iostream.h>
#include<conio.h>
int max=7;
int t=0;
class stack
{
int s[7];
public:
void push(int);
void pop();
void top();
void empty();
void show();
};
void stack::push(int y) //Push Operation
{
if(t<max)
{
t=t+1;
s[t]=y;
}
else
cout<<endl<<"stack overflows..."<<endl;
}
void stack::pop() //Pop Operation
{
int item;
if(t>=0)
{
t=t-1;
item=s[t+1];
cout<<endl<<"popped item >>"<<item<<endl;
}
else
cout<<endl<<"stack underflows"<<endl;
}
void stack::top() //To find the top of the stack
{
if(t>=0)
cout<<endl<<"topmost element >> "<<s[t]<<endl;
else
cout<<endl<<"stack underflows..."<<endl;
}
void stack::empty() //To check if the stack is empty
{
if(t<0)
cout<<endl<<"stack is empty..."<<endl;
else
cout<<endl<<"stack is not empty..."<<endl;
}
void main()
{
int a,x;
stack s1;
clrscr();
do
{
cout<<"enter an option..."<<endl<<"1-push"<<endl<<"2-pop"<<endl<<"3-top"<<endl<<"4-empty"<<endl;
cout<<"5-end"<<endl;
cin>>a;
cout<<endl;
switch(a)
{
case 1:
{
cout<<endl<<"enter a value >> "<<endl;
cin>>x;
s1.push(x);
}
break;
case 2:
s1.pop();
break;
case 3:
s1.top();
break;
case 4:
s1.empty();
break;
}
}
while(a!=5);
getch();
}
C++ program -Binary search using Class
#include <iostream.h>
#include <conio.h>
class bin_search
{
int num[50],s,search;
public:
void getdata(void);
int searchfun(void);
void display(void);
};
void bin_search :: getdata(void)
{
cout<<endl<<endl;
cout<<"Enter how many Number of array you want to create:-";
cin>>s;
cout<<"\nEnter "<<s<<" Integers in Ascending order\n";
for(int i=0;i<s;i++)
cin>>num[i];
cout<<"\nEnter your Search :- ";
cin>>search;
}
int bin_search :: searchfun(void)
{
int bottom=0,top=s-1,mid;
while(top >= bottom)
{
mid=(top+bottom)/2;
if(num[mid]==search)
return(mid+1);
else
{
if(num[mid] < search)
bottom=mid+1;
else
top=mid;
}
}
return(-1);
}
void bin_search :: display(void)
{
int result;
result=searchfun();
if(result==-1)
cout<<"\n\nEntered search is Invalid\n";
else
cout<<"\n\nSearch is Located at "<<result<<" Postition\n";
}
void main()
{
clrscr();
bin_search o1;
o1.getdata();
o1.display();
getch();
}