Complete C++ Practical File
Complete C++ Practical File
void main()
{
cout<<”Your introduction”;
cout<<”your institute introduction” ;
getch();
}
Output :
2. write a c++ program that prompts the user to input three integer value and print these
values in forward and reversed order.
Solution :
#include<iostream.h>
#include<conio.h>
Void main()
{
int num,rev;
clrscr();
cout<<”enter the 3 digit integer number :”;
cin>>num;
cout<<”entered number :”<<num;
cout<<”reverse :”
while(num!=0)
{
rev=num%10;
num=num/10;
cout<<rev;
}
getch();
}
Output:
3. Write a program to display the following output using a single cout statement.
Subject Marks
Mathematics 90
Computer 77
Chemistry 69
Solution :
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
getch();
}
Output :
4. Write a program which accept principle, rate and time from user and print the simple
interest.
Solution :
#include<iostream.h>
#include<conio.h>
void main()
{
int principle,rate,time,interest;
clrscr();
getch();
}
Output :
5. Write a program to swap the values of two variables.
Solution :
#include<iostream.h>
#include<conio.h>
void main()
{
int x, y,temp;
cout<<"Enter Value of x :";
cin>>x;
cout<<"\nEnter Value of y :";
cin>>y;
temp = x;
x = y;
y = temp;
Output :
5. Write a program to check whether the given number is even or odd (using?: ternary
Operator).
Solution :
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
(n % 2 == 0) ? cout << n << " is even." : cout << n << " is odd.";
getch();
}
Output :
7.Write a program to swap value of two variables without using third variable..
Solution :
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5, b=10;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
getch();
}
Output :
9.Write a C++ program to add two complex numbers using class.
Solution :
#include<iostream.h>
#include<conio.h>
class Complex {
public:
int real;
int imag;
Complex()
{
real = 0;
imag = 0;
}
Complex(int r, int i)
{
real = r;
imag = i;
}
Complex res;
return res;
}
};
void main()
{
cout << "Complex number 1 : " << C1.real << " + i" << C1.imag << endl;
Complex C3;
C3 = Addition(C1, C2);
cout << "Sum of complex no : real :“ << C3.real << " , imag:" << C3.imag;
cout << endl;
getch();
}
Output :
9. Write a c++ program to Adding two times using class.
Solution :
#include<iosteam.h>
#include<conio.h>
class Time {
private:
int hours;
int minutes;
int seconds;
public:
void getTime(void);
void putTime(void);
void addTime(Time T1, Time T2);
};
void Time::getTime(void)
{
cout << "Enter time:" << endl;
cout << "Hours? ";
cin >> hours;
cout << "Minutes? ";
cin >> minutes;
cout << "Seconds? ";
cin >> seconds;
}
void Time::putTime(void)
{
cout << endl;
cout << "Time after add: ";
cout << hours << ":" << minutes << ":" << seconds << endl;
}
void main()
{
Time T1, T2, T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1, T2);
T3.putTime();
getch();
}
Output :
#include<stdio.h>
#include<conio.h>
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}
void main()
{
print(10);
print(10.10);
print("ten");
getch();
}
Output :
#include<iostream.h>
#include<conio.h>
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
void main()
{
Car obj;
getch();
}
Output :
#include<stdio.h>
#include<conio.h>
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
void main()
{
Car obj;
getch();
}
Output :
#include<iostream.h>
#include<conio.h>
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
void main()
{
Car obj;
getch();
}
Output :
#include<iostream.h>
#include<conio.h>
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
void main()
{
Car obj1;
Bus obj2;
getch();
}
Output :
#include<stdio.h>
#include<iostream.h>
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
void main()
{
Bus obj2;
getch();
}
Output :
16. Write a program for copy constructor.
Solution :
#include <iostream.h>
#include<conio.h>
class Wall {
private:
double length;
double height;
public:
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * height;
}
};
void main()
{
Wall wall1(10.5, 8.6);
Wall wall2 = wall1;
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
getch();
}
Output :
17. Write a program for parameterised.
Solution :
#include <iostream.h>
#include<conio.h>
class Wall {
private:
double length;
double height;
public:
Wall(double len, double hgt) {
length = len;
height = hgt;
}
double calculateArea() {
return length * height;
}
};
void main() {
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
getch();
}
Output :
18. Write a program for default constructor.
Solution :
#include<stdio.h>
#include<conio.h>
class Wall {
private:
double length;
public:
Wall() {
length = 5.5;
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
void main()
{
Wall wall1;
getch();
}
Output :
19. Write a program for dynamic constructor.
Solution :
#include<stdio.h>
#include<conio.h>
class geeks {
const char* p;
public:
geeks()
{
p = new char[6];
p = "geeks";
}
void main()
{
geeks obj;
obj.display();
}
getch();
}
Output :
20. Write a program for friend funtion.
Solution :
#include<iostream.h>
#include<conio.h>
class demo
{
private:
int a=5;
friend void display();
};
void display()
{
cout<<"a: "<<d.a<<endl;
}
void main()
{
demo d;
display(d);
getch();
}
Output :