Professional Documents
Culture Documents
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{
clrscr();
int arr[]={1,9,2,8,2,8,1,9};
int n= sizeof(arr)/sizeof(arr[0]);
int visited[10];
for(int i=0;i<n;i++)
{
if(visited[i]!=1)
{
int count=1;
for(int j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
count++;
visited[j]=1;
}
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
int a[10],i,n,ele,p;
if(p>n)
{
cout<<"Invalid position";
}
else
{
for(i=n-1;i>=p; i--)
{
a[i+1]=a[i];
}
a[p]=ele;
n=n+1;
for(i=0;i<n;i++)
{
cout<<setw(4)<<a[i];
}
}
getch();
}
Output:
Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
clrscr();
int a[10],i,n,ele,p;
ele=a[p];
for(i=p;i<n;i++)
a[i]=a[i+1];
n=n-1;
cout<<"The elements after the deletion is ";
for(i=0;i<n;i++)
cout<<setw(4)<<a[i];
}
getch();
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],n,i,j,temp;
for(i=1;i<n;i++)
{
j=i;
while(j>=1)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
j=j-1;
}
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,n,m,b,loc,e,pos,ele;
clrscr();
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter the elements: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the search element: ";
cin>>ele;
pos=-1;
b=0;
e=n-1;
while(b<=e)
{
m=(b+e)/2;
if(ele==a[m])
{
pos=m;
break;
}
else
{
if(ele<a[m])
e=m-1;
else
b=m+1;
}
}
if(pos>=0)
cout<<"Position: "<<pos;
else
cout<<"Search is unsuccessful";
getch();
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
class SimpleInterest
{
private:
float p,r,t,si;
public:
void readdata();
void compute();
void display();
};
void SimpleInterest::compute()
{
si=(p*t*r)/100;
}
void SimpleInterest::display()
{
cout<<"Principal: "<<p<<endl;
cout<<"Rate: "<<r<<endl;
cout<<"Time: "<<t<<endl;
cout<<"Simple Interest is: "<<si<<endl;
}
void main()
{
SimpleInterest si;
clrscr();
si.readdata();
si.compute();
si.display();
getch();
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Quadratic
{
private:
int a,b,c;
float disc,x,x1,x2;
public:
void readdata();
void compute();
void display();
};
void Quadratic::readdata()
{
cout<<"Enter the values of a,b,c: "<<endl;
cin>>a>>b>>c;
}
void Quadratic::compute()
{
disc=b*b-4*a*c;
}
void Quadratic::display()
{
compute();
if(disc==0)
{
cout<<"Equal Roots: "<<endl;
x=-b/(2*a);
cout<<"Roots is: "<<x;
}
else if(disc>0)
{
cout<<"Real and Distinct Roots: "<<endl;
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
cout<<"Root1: "<<x1<<endl;
cout<<"Root2: "<<x2<<endl;
}
else
{
cout<<"Imaginary Roots"<<endl;
}
}
void main()
{
Quadratic q;
clrscr();
q.readdata();
q.compute();
q.display();
getch();
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class fnld
{
public:
int area(int a)
{
return a*a;
}
void main()
{
clrscr();
fnld f1;
cout<<"Area of square: "<<f1.area(5)<<endl;
cout<<"Area of triangle: "<<f1.area(3.0,5.5)<<endl;
cout<<"Area of rectangle: "<<f1.area(4.5,3)<<endl;
getch();
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
x=cube(5);
cout<<"Cube of 5 is: "<<x<<endl;
y=cube(10);
cout<<"Cube of 10 is: "<<y<<endl;
getch();
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class Series
{
private:
int sum,x,n;
public:
Series(int y, int m)
{
sum=1;
x=y;
n=m;
}
int sumseries();
};
int Series::sumseries()
{
for(int i=1;i<=n;i++)
{
sum=sum+pow(x,i);
}
return sum;
}
void main()
{
int x,n;
clrscr();
cout<<"Enter the base value i.e, X value: "<<endl;
cin>>x;
Series s1(x,n);
Output:
Program 11: Create a base class containing the data member roll
number and name. Also create a member function to read and display
the data using the concept of single level inheritance. Create a derived
class that contains marks of two subjects and total marks as the data
members.
Code:
#include<iostream.h>
#include<conio.h>
class base
{
private:
int rollno;
char name[20];
public:
void read()
{
cout<<"Enter Roll Number and Name"<<endl;
cin>>rollno>>name;
}
void display()
{
cout<<"Roll Number: "<<rollno<<endl;
cout<<"Name: "<<name<<endl;
}
};
class derive: public base
{
private:
int m1,m2,t;
public:
void read1()
{
cout<<"Enter first and second marks: "<<endl;
cin>>m1>>m2;
t=m1+m2;
}
void display1()
{
cout<<"First marks: "<<m1<<endl;
cout<<"Second marks: "<<m2<<endl;
cout<<"Total marks: "<<t<<endl;
}
};
void main()
{
derive ob;
clrscr();
ob.read();
ob.read1();
ob.display();
ob.display1();
getch();
}
Output:
Code:
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class std
{
private:
int rno;
char name[25];
float fees;
public:
void get();
void display();
};
void std::get()
{
cout<<"Enter the register number: ";
cin>>rno;
void std::display()
{
cout<<"Register Number: "<<rno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Fees: "<<fees<<endl;
}
void main()
{
std s, *sp;
sp=&s;
clrscr();
sp->get();
sp->display();
getch();
}
Output:
Code:
#include<iostream.h>
#include<conio.h>
int s[100], n=100, top=-1;
void pop()
{
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else
{
cout<<"The popped element is: "<<s[top]<<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"Stack elements are:";
for(int i=top;i>=0;i--)
{
cout<<s[i]<<" ";
}
}
else
{
cout<<"Empty Stack\n";
}
}
int main()
{
clrscr();
int ch,val;
cout<<"1.PUSH"<<endl;
cout<<"2.POP"<<endl;
cout<<"3.DISPLAY"<<endl;
cout<<"4.EXIT"<<endl;
do
{
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the element: ";
cin>>val;
push(val);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: cout<<"EXIT"<<endl;
break;
default: cout<<"INVALID CHOICE"<<endl;
}
}while(ch!=4);
return 0;
}
Output: