Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
1. A C++ Program to check whether the given no is a Prime
Number or not.
//Program of prime number
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i=1,t=0;
cout<<"\n Enter any number:";
cin>>n;
while(i<=n)
{
if(n%i==0)
t++;
i++;
}
if(t==2)
cout<<"\n Number is prime";
else
cout<<"\n Number is not prime";
getch();
}
Output:
1
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
2. A C++ Program to check whether the given no is a Perfect
Number or not.
//Program of perfect number
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,s=0,i;
cout<<"\n Enter any number:";
cin>>n;
for(i=1;i<=(n/2);i++)
{
if(n%i==0)
s+=i;
}
if(s==n)
cout<<"\n Number is perfect";
else
cout<<"\n Number is not perfect";
getch();
}
Output:
2
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
3. A C++ Program to check whether the given number is an
Armstrong number or not.
//Program of armstrong
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,s=0,b=0,r;
cout<<"\n Enter number:";
cin>>n;
while(n>0)
{
r=n%10;
b=b*10+r;
n=n/10;
s=(s+(r*r*r));
if(s==n)
{
cout<<"\n Number is an armstrong";
}
else
{
cout<<"\n Number is not an armstrong";
}
cout<<"\n Sum="<<s;
}
getch();
}
Output:
3
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
4. A C++ Program to find the roots of a Quadratic equation.
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
float a,b,c,x,y;
cout<<"\n Value of A=";
cin>>a;
cout<<"\n Value of B=";
cin>>b;
cout<<"\n Value of C=";
cin>>c;
x= (-b-sqrt(b*b-4*a*c))/2*a;
cout<<"\n Value of first root is="<<x;
getch();
y= (-b+sqrt(b*b-4*a*c))/2*a;
cout<<"\n Value of second root is="<<y;
getch();
}
Output
4
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
5. A C++ Program to calculate the L.C.M. and H.C.F. of two
given numbers.
//Program of Lcm and Hcf
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,hcf=1,lcm,i,j;
clrscr();
cout<<"Enter the value of a,b:";
cin>>a>>b;
for(i=2;i<a*b;i++)
{
if((a%i==0)&&(b%i==0))
{
hcf=i;
}
}
lcm=(a*b)/hcf;
cout<<"\n Lcm="<<lcm;
cout<<"\n Hcf="<<hcf;
getch();
}
Output:
5
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
6. A C++ program to generate the Fibonacci Series up to n with
the help of constructor.
//To generate Fibonacci series with the help of constructor
#include<iostream.h>
#include<conio.h>
class Test
{
int a;
int b;
int c;
int n;
public:
Test()
{
a=0;
b=1;
c=0;
}
void input()
{
cout<<"\n Enter Last number:";
cin>>n;
}
void output()
{
while(c<=n)
{
cout<<c<<'\t';
a=b;
b=c;
c=a+b;
}
}
};
void main()
{
Test t;
clrscr();
t.input();
t.output();
getch();
}
Output
6
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
7. A C++ Program to calculate the NCR using function facto().
//Program of NCR using function facto()
#include<iostream.h>
#include<conio.h>
class NCR
{
int n;
int r;
long int fn;
long int fr;
long int fnr;
public:
void input();
void output();
long int Facto(int);
};
void NCR::input()
{
cout<<"\n Enter the values for n & r";
cin>>n>>r;
}
void NCR::output()
{
fn=Facto(n);
fr=Facto(r);
fnr=Facto(n-r);
cout<<"\n NCR="<<fn/(fnr*fr);
}
long int NCR::Facto(int n)
{
if(n<2)
return(1);
else
return(n*Facto(n-1));
}
void main()
{
clrscr();
NCR t;
t.input();
t.output();
getch();
}
Output
7
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
8. A C++ Program to implement call by value and call by address
in a single program.
/*A C++ program to implement call by value and call by address*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
void swap1(int,int);
void swap2(int*,int*);
cout<<"Enter the value of a and b:";
cin>>a>>b;
cout<<"Before calling any function: A="<<a<<", B="<<b;
swap1(a,b); //Call by value
cout<<"\nAfter calling swap1 function: A="<<a<<", B="<<b;
swap2(&a,&b); //Call by address
cout<<"\nAfter calling swap2 function: A="<<a<<", B="<<b;
getch();
}
void swap1(int a,int b)
{
int k;
k=a;
a=b;
b=k;
cout<<"\nIn function swap1: A="<<a<<", B="<<b;
}
void swap2(int *x,int *y)
{
int k;
k=*x;
*x=*y;
*y=k;
cout<<"\nIn function swap2: A="<<*x<<", B="<<*y;
}
8
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
9. A C++ program to create a class employee and display all data
related to employee using inheritance.
//Program of Employee using class
#include<iostream.h>
#include<conio.h>
class Employee
{
protected:
char name[20];
int empno;
};
class Salary:public Employee
{
protected:
int bs;
int hra;
int da;
int ta;
int itax;
};
class Report:public Salary
{
int ts;
int gs;
public:
void input()
{
cout<<"\n Enter Name:";
cin.getline(name,20);
cout<<"\n Enter Employee no:";
cin>>empno;
cout<<"\n Enter Basic Salary, hra,da,ta,itax:";
cin>>bs>>hra>>da>>ta>>itax;
}
void output()
{
ts=bs+ta+da-itax;
gs=bs+da+ta;
cout<<"\n Employee Name="<<name;
cout<<"\n Employee number="<<empno;
cout<<"\n HRA="<<hra;
cout<<"\n DA="<<da;
cout<<"\n TA="<<ta;
cout<<"\n TS="<<ts;
cout<<"\n ITAX="<<itax;
cout<<"\n GS="<<gs;
}
};
9
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
void main()
{
clrscr();
Report r;
r.input();
r.output();
getch();
}
Output:
10
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
10. A C++ program to implement hybrid inheritance.
//Program of hybrid inheritance
#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int a,b;
public:
void input()
{
cout<<"\n Enter the value for a&b:";
cin>>a>>b;
}
void output()
{
cout<<"\n A="<<a<<"\n B="<<b;
}
};
class Derive1:public Base
{
protected:
int c;
public:
void input()
{
Base::input();
cout<<"\n Enter the value for c:";
cin>>c;
}
void output()
{
Base::output();
cout<<"\n C="<<c;
}
};
class Base1
{
protected:
int d;
public:
void input()
{
cout<<"\n Enter the value for d:";
cin>>d;
}
void output()
{
cout<<"\n D="<<d;
}
11
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
};
class MDerive:public Derive1,public Base1
{
int e;
public:
void input()
{
Derive1::input();
Base1::input();
cout<<"\n Enter the value for e:";
cin>>e;
}
void output()
{
Derive1::output();
Base1::output();
cout<<"\n E="<<e;
}
};
void main()
{
clrscr();
MDerive t;
t.input();
t.output();
}
Output:
12
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
11. A C++ Program to overload binary * operator.
//Program to overload binary operator *
#include<iostream.h>
#include<conio.h>
class Test
{
int a;
int b;
public:
Test()
{
a=0;
b=0;
}
Test(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nA="<<a<<"\nB="<<b;
}
Test operator*(int t)
{
Test tmp;
tmp.a=a*t;
tmp.b=b*t;
return(tmp);
}
};
void main()
{
clrscr();
Test obj1(32,45),obj2;
obj2=obj1*5;
obj1.display();
obj2.display();
getch();
}
Output
13
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
12. A C++ Program to overload the unary and binary operator
//Program of unary and binary operator overloading
#include<iostream.h>
#include<conio.h>
class number
{
public:
int x;
int y;
number(){}
number(int j,int k)
{
x=j;
y=k;
}
number operator++()
{
++x;
++y;
}
number operator+(number D)
{
number T;
T.x=x+D.x;
T.y=y+D.y;
return T;
}
void show()
{
cout<<"\n X="<<x;
cout<<"\n Y="<<y;
}
};
void main()
{
clrscr();
number A(2,3),B(4,5),C,G(10,20);
A.show();
B.show();
C=A+B;
C.show();
cout<<"\n Before increment of G";
G.show();
++G;
cout<<"\n Ater increment of G";
G.show();
getch();
}
14
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
Output:
15
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
13. A C++ Program to implement the multipath inheritance.
//Program of multipath inheritance
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
char name[20];
int roll;
};
class Fees:virtual public Student
{
protected:
int fees;
};
class Marks:virtual public Student
{
protected:
int pmarks;
};
class Result:public Marks,Fees
{
public:
void input()
{
cout<<"\n Enter name:";
cin.getline(name,20);
cout<<"\n Enter Roll,Fees,Pmarks:";
cin>>roll>>fees>>pmarks;
}
void output()
{
cout<<"\n Name="<<name;
cout<<"\n Roll="<<roll;
cout<<"\n Fees="<<fees;
cout<<"\n Pmrks="<<pmarks;
}
};
void main()
{
clrscr();
Result t;
t.input();
t.output();
getch();
}
Output:
16
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
17
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
14. A C++ program to create a class student and another class
Fees using inheritance.
//Program of inheritance
#include<iostream.h>
#include<conio.h>
class student
{
protected:
char name[20];
int rollno;
};
class Fees:public student
{
float f;
public:
void input()
{
cout<<"\n Enter Name:";
cin.getline(name,20);
cout<<"\n Enter roll number:";
cin>>rollno;
cout<<"\n Enter fees:";
cin>>f;
}
void output()
{
cout<<"\n Name="<<name;
cout<<"\n Roll number="<<rollno;
cout<<"\n Fees="<<f;
}
};
void main()
{
clrscr();
Fees f;
f.input();
f.output();
getch();
}
Output
18
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
19
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
15. A C++ Program to overload binary + operator using friend
function.
//Operator overloading using friend function
#include<iostream.h>
#include<conio.h>
class Ftest
{
int a;
int b;
public:
Ftest()
{
a=0;
b=0;
}
Ftest(int x,int y)
{
a=x;
b=y;
}
friend Ftest operator+(Ftest,Ftest);
void display();
};
void Ftest::display()
{
cout<<"\n A="<<a;
cout<<"\n B="<<b;
}
Ftest operator+(Ftest ob,Ftest ob1)
{
Ftest tmp;
tmp.a=ob.a+ob1.a;
tmp.b=ob.b+ob1.b;
return(tmp);
}
void main()
{
clrscr();
Ftest t,t1(20,56),t2(59,85);
t=t1+t2;
cout<<"\n Object t1 \n";
t1.display();
cout<<"\n Object t2 \n";
t2.display();
cout<<"\n Result \n";
t.display();
}
20
Practical of C++
Name: Devbrat kr. singh Roll No. :- 1863058 Reg No: 184630086 / 2018
Output:
21