GURU NANAK INSTITUTE OF TECHNOLOGY
MULLANA (AMBALA)
Problem solving through C++
Session: 2024-2025
Submitted to: Submitted by:
Miss Khusboo Jagjeet
Roll No.: 6324614
B.C.A. 1styear
INDEX
Sr. No. Name of the Practical Date Remark
1. Write a program to print the following lines: Your
introduction and
your institute introduction.
2. Write a program that accepts principal, rate and time
from the user and prints the simple interest.
3. Write a program to swap the values of two variables.
4. Write a program to check whether the given number is
even or odd using ternary operator.
5. Write a program to check whether the given number is
positive or negative using ternary operator.
6. Write a program that inputs three numbers and displays
the largest number using ternary operator.
7. WAP to initialize data members of the class using the
constructor.
8. Pass values to the constructor and initialize the members
of that class to those values.
9. Create a class called cube with the data members length,
breath, height and member function to accept the details,
to calculate the volume and to display the details.
10. WAP to calculate the sum using constructor overloading.
11. WAP to demonstrate the use of detractor.
12. Create a program to show the order of constructor and
destructor.
13. Program to find the number of vowels, consonants, digits
and white spaces in a string.
14. Program to multiply two matrices by passing matrix to a
function.
15. Increment ++ and decrement – operator overloading.
16. Program to add two complex numbers.
17. Program to show function overriding.
18. Program to show polymorphism in class.
19. Program to show function overloading.
20. Program to show inheritance.
PRACTICAL 1
Aim: Write a program to print the following lines: Your Introduction & Your
Institute introduction.
#include<iostream>
using namespace std;
int main()
char N[20],I[20],D[20];
int R,A;
cout<<’’EnteryourName:”<<endl;
cin>>N;
cout<<”EnteryourAge:”<<endl;
cin>>A;
cout<<”EnteryourRollno.:”<<endl;
cin>>R;
cout<<”EnteryourInstitute:”<<endl;
cin>>I;
cout<<”EnteryourDegree:”<<endl;
cin>>D;
return 0;
}
Output:
EnteryourName : Jagjeet
EnteryourAge: 19
EnteryourRollno.: 14
EnteryourInstitute : Guru Nanak Institute
EnteryourDegree : BCA
PRACTICAL 2
Aim: Write a program that accepts principal, rate, and time from the user
and prints the simple interest.
#include<iostream>
using namespace std;
int main()
float principal, rate, time, simple Interest;
cout<<”Enter the principal amount:”;
cin>>principal;
cout<<”Enter the rate of interest (in percentage):;
cin>>rate;
cout<<”Enter the time period (in year):;
cin>>time;
Simple Interest= (principal*rate*time)/100;
cout<<”simple Interest :”<< simple Interest<<endl;
return 0;
Output:
Enter the principal amount: 390
Enter the rate of Interest (in percentage): 55
Enter the time period (in year): 4
Simple Interest: 858
PRACTICAL 3
Aim: Write a program to swap the values of two variables.
#include<iostream>
using namespace std;
int main()
int a, b, temp;
cout<<”Enter value for a:”:
cin>>a;
cout<<”Enter value of b:”;
cin>>b;
temp = a;
a = b;
b = temp;
cout<<”After swapping:\n”;
cout<<” a = “ << a <<”\n”;
cout<<” b =”<< b <<”\n”;
return 0;
Output:
Enter value for a: 9
Enter value for b: 10
After swapping:
a = 10
b=9
PRACTICAL 4
Aim: Write a program to check whether the given number is even or odd
using ternary operator.
#include<iostream>
using namespace std;
int main()
int a;
cout<<”Enter the value of a:”;
cin>>a;
(a%2==0)?cout<<”even”:cout<<”odd”;
return 0;
Output:
Enter the value of a: 7
odd
PRACTICAL 5
Aim: Write a program check whether the given number is positive or negative
using ternary operator.
#include<iostream>
using namespace std;
int main()
int a;
cout<<”Enter the value of a:”;
cin>>a;
(a>0)?cout<<”positive”:cout<<”negative”;
return 0;
Output:
Enter the value of a: -9
negative
PRACTICAL 6
Aim: Write a program that inputs three numbers and display the largest
number using ternary operator.
#include<iostream>
using namespace std;
int main()
int a, b, c, max;
cout<<”Enter the three numbers:”;
cin>>a>>b>>c;
max= (a>b)?(a>c?a:c)(b>c?b:c);
cout<<”The largest number is :”<<max;
return 0;
Output:
Enter the three numbers: 7 8 6
The largest number is: 8
PRCTICAL 7
Aim: WAP to initialized data members of the class using the constructor.
#include<iostream>
#include<string>
using namespace std;
classStudent
{
private: stringname;
int age;
float gpa;
public:
Student(string, int a, float g)
{
name= n; age= a; gpa= g;
}
void display()
{
cout <<”Name:”<<name<<endl;
cout<<”Age”<<age<<endl;
cout<<”GPA”<<gpa<<endl:
int main()
{
Student S1(“Jagjeet”, 19, 3.7);
cout<<”Student Details:”<<endl;
S1.display();
return 0 ;
}
Output:
Student Details:
Name: Jagjeet
Age: 19
GPA: 3.7
PRACTICAL 8
Aim: Pass values to the constructor and initialize the members of that class to
those values.
#include<iostream>
using namespace std;
class Person
{
private:
string name;
int age;
public:
Person(string n, int a)
{
Name= n;
Age= a;
void display()
{
cout<<”Name”<<name<<endl;
cout<<”Age”<<age<<endl;
}
}
int main()
{
Personp(“Jagjeet”, 19);
p.display();
return 0;
}
Output:
Name: Jagjeet
Age: 19
PRACTICAL 9
Aim: Create a class called Cube with the data members Length, Breadth,
Height and member functions to accepted the details, calculate the Volume,
and to display the details.
#include<iostream>
using namespace std;
class Cube
{
double l, b, h;
public:
void getdata()
{
cout<<”Enter Length, Breadth, Height:”;
cin>>l>>b>>h;
}
double volume()
{
returnl*b*h:
}
void putdata()
{
cout<<”Length:”<<l<<”\nBreadth:”<<b<<\nHeight:”<<h;
cout<<”\nVolume:”<< volume() << endl;
}
};
int main()
Cube c;
c.getdata();
c.putdata();
return 0;
}
Output:
Enter Length, Breadth, Height: 15 25 35
Length: 15
Breadth: 25
Height: 35
Volume: 13125
PRACTICAL 10
Aim: WAP to calculate the sum using constructor overloading.
#include<iostream>
using namespace std;
class sum
int result;
public:
sum()
{
result=0;
sum(int a)
result= a;
sum(int a, int b)
result= a+b;
void display()
{
cout<<”The sum is:”<<result<<endl;
};
int main(){
Sums1;Sums2(9);Sums3(10,15);
S1.display();
S2.display();
S3.display();
return 0;
Output:
The sum is: 0
The sum is: 9
The sum is: 25
PRACTICAL 11
Aim: WAP to demonstrate the use of destructor.
#include<iostream>
using namespace std;
int count= 0;
classdemo
{
public:
demo()
{
count++;
cout<<”no. of objects reated”<<count;
}
~demo()
{
count--;
cout<<”no. of objects destroyed”<<count;
}
};
int main()
{
demo a, b, c;
{
demod;
}
retrun 0;
}
Output:
no. of objects created1
no. of objects created2
no. of objects created3
no. of objects created4
no. of objects destroyed3
no. of objects destroyed2
no. of objects destroyed1
no. of objects destroyed0
PRACTICAL 12
Aim: Create a program to show the order of constructor and destructor.
#include<iostream>
using namespace std;
class demo
public:
Demo()
cout<<”Constructor called”<<endl:
}
}
~Demo()
cout<<”Destructor called”<<endl;
void createObjectInFunction()
Demo d2;
cout<<”Inside function createObjectInFunction”<<endl;
int main()
cout<<”Main begins”<<endl;
Demo d1;
createObjectInFunction();
cout<<”Back in main”<<endl;
return 0;
Output:
Main begins
Constructor called
Constructor called
Inside function createObjectInFunction
Destructor called
Back in main
Destructor called
PRACTICAL 13
Aim: Program to find the number of Vowels, Consonants, Digits, and white
spaces in a string.
#include<iostream>
#include<cctype>//For character classification function
using namespace std;
int main()
{
string input;
int vowels=0,consonants=0,digits=0,spaces=0,
cout<<”Enter a string: “;
getline(cin,inputs);
for(char c: input) {c =to lower(c);
if(c==’a’||c==’e’||c==’I’||c==’o’||c==’u’){++vowels;
}
else if (is alpha(c)){++consonants;
}
else if (is digit(c)){++digits;
}
else if (is spaces(c)){++spaces;
}
}
cout<<”Vowels: “<<vowels<<endl;
cout<<”Consonants:”<<consonatas<<endl;endl;
cout<<”Digits: “<<digits<<endl;
cout<<”White spaces:”<<spaces<<endl;
return 0;}
Output:
Enter a string: hello world 765
Vowels: 3
Consonants: 7
Digits: 3
Spaces: 2
PRACTICAL 14
Aim: Program to multiply two matrices by passing matrix to a function.
#include<iostream>
using namespace std;
void enter data(intA[][10],intB[][10],intr1,intc1,intr2,intc2);
void multiply matrices(int A [][10], int B [][10],int R [][10],int r1,int c1,int r2,int c2);
void display (int mult [][10],int r1,int c2);
int main()
{
int A[10][10],B[10][10],mult[10][10];
Int r1,c1,r2,c2;
cout<<”Enter rows and columns for first mateix:”;
cin>>r1>>c1;
cout<<”Enter the rows and columns for second matrix:”;
cin>>r2,c2;
while(c1!=r2)
{
cout<<”Error! Column of first matrix not equal to row of second matrix”endl;
cout<<”Enter rows and columns for first matrix:”;
cin>>r1>>r2;
cout<<”Enter the rows and columns for second matrix:”;
cin>>r2>>c2;
}
enter data(A,B,mult,r1,c1,r2,c2);
multiply matrices(A,B,r1,c1,r2,c2);
display(mult,r1,c2);
return 0;}
void enter data(int A [][10],int B []10],int r1,int c1,int r2,int c2)
{
cout<<”Enter the elements of matrix 1:”<<endl;
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1,j++)
{
cin>>A[i][j];
}
}
cout<<”Enter the elements of matrix 2:”<<endl;
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
cin>>B[i][j];
}
}
}
void multiply matrices(int A[][10],int B[][10],int mult[][10],int r1,int c1,int r2,int c2)
{
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mult[i][j]=0;
for(int k=0;k<c1;k++)
{
mult[i][j]+=A[i][k]*B[k][j];
}
}
}
}
void display(int mult[][10],int r1,int c2)
{
cout<<”resultant matrix”<<endl;
for(int i=0;i<r1;i++)
{
for(int j=0;jv<c2;j++)
{
cout<<mult[i][j]<<”\t”;
cout<<endl;
}
}
Output:
Enter rows and columns for first matrix: 3 2
Enter the rows and columns for second matrix: 2 3
Enter the elements of matrix 1:
1 9
4 7
6 8
Enter the elements of matrix 2:
4 4 3
5 9 6
resultant matrix
49 85 57
51 79 54
64 96 66
PRACTICAL 15
Aim: Increment ++ and decrement - - operator overloading.
#include<iostream>
using namespace std;
class demo
{
int x;
public:
void getdata()
{
cout<<”Enter a number:”:
cin>>x;
void putdata()
{
cout<<x;}
void operator++()
{
x=x+1;
}
};
Int main()
{
demo a;
a.getdata();
cout<<”original value=”;
a.putdata();
cout<<endl;
++a;
cout<<”value after increment=”;
a.putdata();
return 0;
}
Output:
Enter a number: 12
original value= 12
value after increment= 13
PRACTICAL 16
Aim: Write C++ program to show function overriding.
#include<iostream>
using namespace std;
class A
{
public:
void display()
{ cout<<”baseclass”;
}
}:
class B:public A
{
public:
void display()
{
cout<<”derives class”;
}
};
int main(){
B obj;
Obj.display();
return 0;
}
Output:
derives class
PS C: \Users\Dell\Desktop\C++
PRACTICAL 17
Aim: Program to add two complex numbers.
#include<iostream>
using namespace std;
complex
{
int r,I;
public:
void getdata()
{
cout<<”Enter real and imag.numbers”;
cin>>r>>I;
}
void putdata()
cout<<”Real=”<<r<<”Imag.=”<<I;
complex operator+(complex b)
complex c;
c.r=r+b.r;
c.i=i+b.i;
return c;
};
int main c()
{
complex a, b, c;
a.getdata();
b.getdata();
c=a+b;
a.putdata();
b.putdata();
cout<<endl;
c.putdata();
return 0;
}
Output:
Enter real and imag. numbers 9 7
Enter real and imge. numbers 8 9
Real= 9 Imag= 7 Imag= 8Imag= 9
Real= 17 Imag= 16
PRACTICAL 18
Aim: Program to show polymorphism in class.
#include<iostream>
using namespace std;
demo
{
int a,b;
public:
void getdata()
{
cout<<”Enter two numbers:”;
cin>>a>>b;
}
void putdata()
{
cout<<a;
}
demo operator+(demo)
{
demo c;
c.a=a+b.a;
return 0;
}
};
int main()
{
demo a, b, c;
a.getdata();
b.getdata();
c= a+b;
a.putdata();
b.putdata();
c.putdata();
return 0;
}
Output:
10
PRACTICAL 19
Aim: Program to show function overloading.
#include<iostream>
using namespace std;
int main()
add(int a, int b)
{
return a+b;
}
double add(double a, double b)
{
return a+b;
}
int add(int a, int b, int c)
{
return a+b+c;
}
int main()
{
cout<<add(8,9)<<endl;
cout<<add(6,4)<<endl;
cout<<add(4,5,6)<<endl;
return 0;
}
Output:
14
18
13.2
PRACTICAL 20
Aim: Program to show inheritance.
#include<iostream>
using namespace std;
class A
{
protected:
int a;
protected:
intx;
public:
void input()
{
cout<<”Enter the value of x:”;
cin>>x;
}
};
classB:publicA
{
int y;
public:
void getdata()
{
cout<<”Enter the value of y:”
cin>>Y;
}
void putdata()
{
cout<<x+y;
}
};
int main()
{
Bb;
b.input();
b.getdata();
b.putdata();
return 0;
}
Output:
Enter the value of x: 9
Enter the value of y: 9
18