C++ LAB FILE
C++ LAB FILE
By Vivek Tyagi
Lab-File
2
By Vivek Tyagi
int main()
{
int a=5, b=10;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
a=a*b; //a=50 (5*10)
b=a/b; //b=5 (50/10)
a=a/b; //a=10 (50/5)
cout<<"After swap a= "<<a<<" b= "<<b<<endl;
return 0;
}
Output:
Before swap a= 5 b= 10
After swap a= 10 b= 5
3
By Vivek Tyagi
int main() {
int a = 1, b = 2, c = 1;
float discriminant, realPart, imaginaryPart, x1, x2;
if (a == 0) {
cout << "This is not a quadratic equation";
}else {
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << x1 << endl;
cout << "Root 2 = " << x2 << endl;
} else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "Root 1 = Root 2 =" << x1 << endl;
}else {
realPart = (float) -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + " << imaginaryPart <<
"i" <<end;
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i"
<<end;
}
}
return 0;
}
Output
Roots are real and same.
Root 1 = Root 2 =-1
4
By Vivek Tyagi
#include <iostream.h>
#include<conio.h>
int main() {
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are alread
y printed
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
return 0;
}
Output:
Enter the number of elements: 10
0 1 1 2 3 5 8 13 21 34
5
By Vivek Tyagi
#include <iostream.h>
#include<conio.h>
int main()
{
int n, i, m=0, flag=0;
cout << "Enter the Number to check Prime: ";
cin >> n;
m=n/2;
for(i = 2; i <= m; i++)
{
if(n % i == 0)
{
cout<<"Number is not Prime."<<endl;
flag=1;
break;
}
}
if (flag==0)
cout << "Number is Prime."<<endl;
return 0;
}
Output:
Enter the Number to check Prime: 17
Number is Prime.
Enter the Number to check Prime: 57
Number is not Prime.
6
By Vivek Tyagi
#include<iostream.h>
#include<conio.h>
int main ()
{
int m, n, p, q, i, j, A[5][5], B[5][5], C[5][5];
cout << "Enter rows and column of matrix A : ";
cin >> m >> n;
cout << "Enter rows and column of matrix B : ";
cin >> p >> q;
if ((m != p) && (n != q))
{
cout << "Matrices cannot be added!";
exit(0);
}
cout << "Enter elements of matrix A : ";
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> A[i][j];
cout << "Enter elements of matrix B : ";
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
cin >> B[i][j];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
C[i][j] = A[i][j] + B[i][j];
cout << "Sum of matrices\n";
for (i = 0; i < m; i++)
{ for (j = 0; j < n; j++)
cout << C[i][j] << " ";
cout << "\n";
}
return 0;
}
7
By Vivek Tyagi
Output
Enter rows and column of matrix A : 2 2
Enter rows and column of matrix B : 2 2
Enter elements of matrix A : 1 2 3 4
Enter elements of matrix B : 4 3 2 1
Sum of matrices
5 5
5 5
Case 2 :
Enter rows and column of matrix A : 2 3
Enter rows and column of matrix B : 3 2
Matrices cannot be added!
Case 3 :
Enter rows and column of matrix A : 1 3
Enter rows and column of matrix B : 1 3
Enter elements of matrix A : 0 2 7
Enter elements of matrix B : 0 3 3
Sum of matrices
0 5 10
8
By Vivek Tyagi
#include <iostream.h>
#include<conio.h>
class class_name
{
private:
int a,b;
public:
class_name(int aa, int bb)
{
cout<<"Constructor is called"<<endl;
a = aa;
b = bb;
cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
cout<<endl;
}
~class_name()
{
cout<<"Destructor is called"<<endl;
cout<<"Value of a: "<<a<<endl;
cout<<"Value of b: "<<b<<endl;
}
};
int main()
{
class_name obj(5,6);
return 0;
}
Output
Constructor is called
Value of a: 5
9
By Vivek Tyagi
Value of b: 6
Destructor is called
Value of a: 5
Value of b: 6
10
By Vivek Tyagi
#include <iostream.h>
#include <string.h>
#include <conio.h>
class Student
{
private: std::string name;
std::string studentClass;
int rollNumber;
double marks;
public:
Student(const std::string & studentName,
const std::string & sClass, int rollNum, double studentMarks):
name(studentName),
studentClass(sClass),
rollNumber(rollNum),
marks(studentMarks) {}
std::string calculateGrade() {
if (marks >= 90) {
return "A+";
} else if (marks >= 80) {
return "A";
} else if (marks >= 70) {
return "B";
} else if (marks >= 60) {
return "C";
} else {
return "D";
}
}
int main() {
// Create a student object
std::string studentName;
std::string sClass;
int rollNum;
double studentMarks;
std::cout << "Student details: ";
std::cout << "\nName: ";
std::getline(std::cin, studentName);
std::cout << "Class: ";
std::getline(std::cin, sClass);
std::cout << "Roll number: ";
std::cin >> rollNum;
std::cout << "Marks (0-100): ";
std::cin >> studentMarks;
Student student(studentName, sClass, rollNum, studentMarks);
student.displayInformation();
return 0;
}
Output:
Student details:
Name: Ljubinka Marquita
Class: V
Roll number: 2
Marks (0-100): 98
Roll Number: 2
Marks: 98
Grade: A+
Student details:
Name: Lia Chimezie
Class: VI
Roll number: 8
Marks (0-100): 88
#include <iostream.h>
#include <conio.h>
class base
{
public:
int x;
void getdata()
{
cout << "Enter the value of x = "; cin >> x;
}
};
class derive : public base
{
private:
int y;
public:
void readdata()
{
cout << "Enter the value of y = "; cin >> y;
}
void product()
{
cout << "Product = " << x * y;
}
};
int main()
{
derive a;
a.getdata();
a.readdata();
a.product();
return 0;
14
By Vivek Tyagi
Output
Enter the value of x = 3
Enter the value of y = 4
Product = 12
15
By Vivek Tyagi
9. Write a C++ program to find area class and over load area using
function overloading.
#include<iostream.h>
#include<stdlib.h>
#include <conio.h>
float area(float r)
{
return(3.14 * r * r);
}
float area(float b,float h)
{
return(0.5 * b * h);
}
float area(float l,float b)
{
return (l * b);
}
int main()
{
float b,h,r,l;
int ch;
do
{
cout<<"\n\n *****Menu***** \n";
cout<<"\n 1. Area of Circle";
cout<<"\n 2. Area of Triangle";
cout<<"\n 3. Area of Rectangle";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
16
By Vivek Tyagi
{
cout<<"\n Enter the Radius of Circle : ";
cin>>r;
cout<<"\n Area of Circle : "<<area(r);
break;
}
case 2:
{
cout<<"\n Enter the Base & Height of Triangle :
";
cin>>b>>h;
cout<<"\n Area of Triangle : "<<area(b,h);
break;
}
case 3:
{
cout<<"\n Enter the Length & Bredth of
Rectangle : ";
cin>>l>>b;
cout<<"\n Area of Rectangle : "<<area(l,b);
break;
}
case 4:
exit(0);
default:
cout<<"\n Invalid Choice... ";
}
}while(ch!=4);
return 0;
}
Output:
1. Area of Circle
1. Area of Circle
2. Area of Triangle
3. Area of Rectangle
4. Exit
Enter Your choice: 1
17
By Vivek Tyagi
2. Area of Triangle
1. Area of Circle
2. Area of Triangle
3. Area of Rectangle
4. Exit
Enter Your choice: 2
Enter the Base & Height of triangle: 6 5
Area of Triangle: 15
3. Area of Rectangle
1. Area of Circle
2. Area of Triangle
3. Area of Rectangle
4. Exit
Enter Your choice: 3
Enter the Length & Breadth of Triangle: 7 9
Area of Rectangle: 63
18
By Vivek Tyagi
#include <iostream.h>
#include <conio.h>
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
return 0;
}
Output:
Default Constructor Invoked
Default Constructor Invoked
19
By Vivek Tyagi
#include <iostream>
#include <conio.h>
class B;
class A
{
int x;
public:
void setdata(int i)
{
x=i;
}
friend void min(A,B);
};
class B
{
int y;
public:
void setdata(int i)
{
y=i;
}
friend void min(A,B);
};
void min(A a,B b)
{
if(a.x<=b.y)
std::cout << a.x << std::endl;
else
std::cout << b.y << std::endl;
}
int main()
{
A a;
B b;
20
By Vivek Tyagi
a.setdata(10);
b.setdata(20);
min(a,b);
return 0;
}
Output:
10
21
By Vivek Tyagi
#include <iostream.h>
class Parent {
int x;
public:
Parent(int i)
{
x = i;
cout << "Inside base class's parameterized "
"constructor"
<< endl;
}
};
#include <iostream.h>
#include <conio.h>
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imag to 0
Complex() : real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
void output() {
if (imag < 0)
cout << "Output Complex number: " << real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag
<< "i";
}
};
23
By Vivek Tyagi
int main() {
Complex complex1, complex2, result;
return 0;
}
Run Code
Output
Enter first complex number:
Enter real and imaginary parts respectively: 9 5
Enter second complex number:
Enter real and imaginary parts respectively: 7 6
Output Complex number: 16+11i
24
By Vivek Tyagi
int main()
{
double numerator, denominator, divide;
cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;
try {
if (denominator == 0)
throw 0;
return 0;
}
Output 1
Enter numerator: 72
Enter denominator: 0
Error: Cannot divide by 0
Output 2
Enter numerator: 72
Enter denominator: 3
72 / 3 = 24
25
By Vivek Tyagi
#include <iostream.h>
#include <conio.h>
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C;
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}
Output:
30
55
26
By Vivek Tyagi
#include <iostream.h>
class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
base1->print();
return 0;
}
Output
Derived Function
27
By Vivek Tyagi
#include<iostream.h>
#include<conio.h>
class vehicle
{
protected:
char name[20];
int wc;
public:
void getdata()
{
cout<<"Enter Vehicle Name:- ";
cin>>name;
cout<<"Enter Wheel Count:- ";
cin>>wc;
}
void outdata()
{
cout<<"\nVehicle is:- "<<name;
cout<<"\nWheel Count:- "<<wc;
}
};
class lightmotor:public vehicle
{
private:
int sl;
public:
void getdata()
{
vehicle::getdata();
cout<<"Enter Speed Limit:- ";
cin>>sl;
}
28
By Vivek Tyagi
void outdata()
{
vehicle::outdata();
cout<<"\nSpeed Limit:- "<<sl;
}
};
class heavymotor:public vehicle
{
private:
int lc;
char per[20];
public:
void getdata()
{
cout<<endl<<endl;
vehicle::getdata();
cout<<"Enter Local Capacity:- ";
cin>>lc;
cout<<"Enter Permit:- ";
cin>>per;
}
void outdata()
{
vehicle::outdata();
cout<<"\nLocal Capacity:- "<<lc;
cout<<"\nPermit:- "<<per;
}
};
void main()
{
lightmotor l;
heavymotor h;
clrscr();
l.getdata();
l.outdata();
h.getdata();
h.outdata();
29
By Vivek Tyagi
getch();
}
30
By Vivek Tyagi
#include <iostream.h>
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
int main()
{
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
31
By Vivek Tyagi
#include <iostream.h>
using namespace std;
class student
{
public:
char a[30];
int roll;
void get()
{
cout<<"Enter the name:"<<endl;
cin>>a;
cout<<"Enter the roll.no:"<<endl;
cin>>roll;
}
};
class mark
{
public:
int mark[4],i;
void in()
{
cout<<"Enter the marks:"<<endl;
for(i=0;i<4;i++)
{
cin>>mark[i];
}
}
};
class process:public student,public mark
{
public:
int t;
float avg;
void calc()
32
By Vivek Tyagi
{
t=mark[0]+mark[1]+mark[2]+mark[3];
avg=t/4;
}
void dis()
{
cout<<"Name:"<<a<<endl;
cout<<"Roll.no:"<<roll<<endl;
cout<<"Marks entered:";
for(i=0;i<4;i++)
{
cout<<mark[i]<<" ";
}
cout<<endl;
cout<<"Total marks:"<<t<<endl;
cout<<"Average:"<<avg<<endl;
}
};
int main()
{
cout<<"\t\tStudent mark list using multiple inheritance"<<endl;
cout<<"\t\
t____________________________________________"<<endl;
process v;
v.get();
v.in();
v.calc();
v.dis();
return 0;
}
Output
Student mark list using multiple inheritance
____________________________________________
Enter the name : Rameshkumar
Enter the roll.no : 87
Enter the marks : 90 89 85 95
Name : Rameshkumar
33
By Vivek Tyagi
Roll.no : 87
Marks entered : 90 89 85 95
Total marks :359
Average :89
34
By Vivek Tyagi
#include <iostream.h>
int main ()
{
//variables initialization
int num, reverse = 0, rem;
num=1234;
cout <<"\nThe number is"<<num;
while(num != 0)
{
rem = num % 10;
reverse = reverse * 10 + rem;
num /= 10;
};
cout<<”Reversed Number: "<<reverse;
getch();
return 0;
}
Output
Reversed Number : 4321
The number is: 1234
Reversed Number: 4321
35
By Vivek Tyagi
#include <iostream.h>
class Shape {
protected:
float dimension;
public:
void getDimension() {
cin >> dimension;
}
virtual float calculateArea() = 0;
};
class Square : public Shape {
public:
float calculateArea() {
return dimension * dimension;
}
};
class Circle : public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension;
}
};
int main() {
Square square;
Circle circle;
cout << "Enter the length of the square: ";
square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;
cout << "\nEnter radius of the circle: ";
circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;
return 0;
}
Output
36
By Vivek Tyagi
#include<iostream.h>
#include<conio.h>
int main()
{
int I,n,fact=1;
clrscr();
cout<<”Enter a no to find factorial=”;
cin>>n;
for(i=1;i<=n;i++)
fact=fact*I;
cout<<”Factorial =”<<fact;
getch();
}
Output.
Enter a no to find factorial=6
120
38
By Vivek Tyagi
#include <iostream.h>
#include <math.h>
int main(){
float number, ans;
clracr();
cout << "Enter any number: ";
cin >> number;
ans = cbrt(number);
cout << "\n Cube Root of " << number << " is: " << ans;
getch();
}
Output
Enter any number: 27
Cube Root of 27 is: 3
40
By Vivek Tyagi
#include <iostream.h>
#include<conio.h>
int main()
{ int n, num, digit, rev = 0;
cltrscr();
cout << "Enter a positive number: ";
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
getch();
return 0;
}
Output
Enter a positive number: 12321
The reverse of the number is: 12321
The number is a palindrome.
#include <iostream.h>
#include<conio.h>
int main()
{
string sentence = "Mary had a little lamb";
int words = 0;
int lenOfSentence = sentence.size();
clrscr();
for(int i = 0; i < lenOfSentence; i++)
{
if(sentence[i] == ' ')
{
words++;
}
}
words = words + 1;
cout << "No. of words = " << words << endl;
}
Output
No. of words = 5
42
By Vivek Tyagi
int main()
{
int a, b, c;
cout << "Enter the three numbers a, b & c" << endl;
cin >> a >> b >> c;
if (a >= b)
{
if (a >= c)
{
cout << "The Largest Among Three Numbers is : "
<< a << endl;
}
else {
cout << "The Largest Among Three Numbers is : "
<< c << endl;
}
}
else { if (b >= c) {
cout << "The Largest Among Three Numbers is : "
<< b << endl;
}
else {
cout << "The Largest Among Three Numbers is : "<< c << endl;
}
}
return 0;
}
Output
Enter the three numbers a, b & c
The Largest Among Three Numbers is : 4196384
43
By Vivek Tyagi
#include<iostream.h>
#include<conio.h>
int main ()
{
int num, temp, rem, sum = 0;
clrscr();
cout << "Enter number to be checked : ";
cin >> num;
temp = num;
while (temp != 0)
{
rem = temp % 10;
sum = sum + rem*rem*rem;
temp = temp / 10;
}
if (sum == num)
cout << "\n" << num << " is an Armstrong number.";
else
cout << "\n" << num << " is not an Armstrong number.";
getch();
return 0;
}
Output
Enter number to be checked : 371
371 is an Armstrong number.
44
By Vivek Tyagi
#include <iostream.h>
#include <conio.h>
int main()
{
char ch='A';
int i, j, k, m;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
cout<<" ";
for(k=1;k<=i;k++)
cout<<ch++;
ch--;
for(m=1;m<i;m++)
cout<<--ch;
cout<<"\n";
ch='A';
}
return 0;
}
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
45
By Vivek Tyagi
#include <iostream.h>
#include <conio.h>
int main()
{
string str;
cout<<"Enter the string ";
getline(cin,str);
lower_string(str); //function call to convert to lowercase
upper_string(str); //function call to convert to uppercase
46
By Vivek Tyagi
return 0;
}
Output:
Enter the string Hola Amigos!
The string in lower case: hola amigos!
The string in upper case: HOLA AMIGOS!
47
By Vivek Tyagi
30. Write a c++ program to read a string and find the number of
vowels in it
#include <iostream>
#include <conio.h>
int main()
{
char str[100] = "prepinsta";
int vowels = 0;
clrscr();
for(int i = 0; str[i]; i++)
{
if(str[i]=='a'|| str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'
||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O' ||str[i]=='U')
{
vowels++;
}
}
cout << "Total Vowels : " << vowels;
return 0;
}
Output
Total Vowels : 3
48
By Vivek Tyagi