TERM WORK
on
OOPS WITH C++
(PCS 307)
2023-24
Submitted to: Submitted by:
SONAL MAM Mohit Prashant
University Roll. No.:
GEHU, D.Dun Class Roll. No./Section: 64/M2
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
GRAPHIC ERA HILL UNIVERSITY, DEHRADUN
ACKNOWLEDGMENT
I would like to particularly thank my OOPS WITH C++ Lab Faculty
Ms. Sonal Mam for her patience, support and encouragement throughout
the completion of this work.
At last but not the least I greatly indebted to all other persons who
directly or indirectly helped me during this course.
Mohit Prashant
University. Roll No.-
B.Tech CSE-III Sem
Session: 2023-24
GEHU, Dehradun
Table of Contents
Program Program Name Page No
No.
1
5
.
6
10
11
12
13
14
15.
Question 1
Q. Write a program in C++ to display following patterns.
(a).
*
**
***
****
*****
Source Code:
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<i+1;j++)
{
cout<<"* ";
}
cout<<"\n";
}
return 0;
}
Output
(b).
*
**
***
****
*****
Source Code:
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5-i;j++)
{
cout<<" ";
}
for(int k=0;k<(i+1);k++)
{
cout<<"* ";
}
cout<<"\n";
}
return 0;
}
Output
(c).
*****
****
***
**
*
Source Code:
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<5;i++)
{
for(int j=5-i;j>0;j--)
{
cout<<"* ";
}
cout<<"\n";
}
}
Output
(d).
*****
****
***
**
*
Source Code:
#include<iostream>
using namespace std;
int main()
{
for(int i=0;i<5;i++)
{for(int j=0;j<i;j++)
{
cout<<" ";
}
for(int k=5-i;k>0;k--)
{
cout<<"* ";
}
cout<<"\n";
}
}
Output
(e).
*
**
***
****
*****
Source Code:
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=5-i;j>0;j--)
{
cout<<" ";
}
for(int k=0;k<i;k++)
{
cout<<"* ";
}
cout<<"\n";
}
return 0;
}
Output
(f).
1
22
333
4444
55555
Source Code:
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=5;i++)
{ for(int j=0;j<i;j++)
{
cout<<i<<" ";
}
cout<<"\n";
}
return 0;
}
Output
(g).
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Source Code:
#include<iostream>
using namespace std;
int main()
{int n=1;
for(int i=1;i<=5;i++)
{ if(n<=9){
for(int j=0;j<i;j++)
{
cout<<n<<" ";
n++;
}
cout<<"\n";
}
else{
for(int j=0;j<i;j++)
{
cout<<n<<" ";
n++;
}
cout<<"\n";
}
}
return 0;
}
Output
Question 2
Write a program in C++ to display the multiplication table vertically from 1 to n.
Source Code:
#include<iostream>
using namespace std;
int main()
{int n;
cout<<"Input the number upto = ";
cin>>n;
cout<<"Multiplication table from 1 to "<<n<<"\n\n";
for(int i=1;i<=10;i++)
{ if(i<10)
{for(int j=1;j<=n;j++)
{if(i*j<10){
cout<<j<<" X "<<i<<" = "<<i*j<<" ";
}
else
cout<<j<<" X "<<i<<" = "<<i*j<<" ";
}
printf("\n");
}
else {
for(int j=1;j<=n;j++)
{
cout<<j<<" X "<<i<<"= "<<i*j<<" ";
}
}
}
return 0;
}
Output
Question 3
Write a program in C++ to diplay the sum of the series
[9+99+999+999+9999….].
Source Code:
#include <iostream>
using namespace std;
int main()
{
int n, i, x = 9;
long int sum = 0;
cout << "\nInput number of terms: ";
cin >> n;
for (i = 1; i <= n; i++)
{
sum += x;
cout << x << " + ";
x = x * 10 + 9;
}
cout << "\nThe sum of the series = " << sum << endl;
}
Output
Program 4
Write a C++ program to sort a given array of 0s, 1s and 2s. In the final array put all
0s first , then all 1s and all 2s in last.
Source Code:
#include <iostream>
using namespace std;
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void sort_012_num(int nums[], int n)
{
int i = 0;
int j = n - 1;
int mid_num = 0;
while (mid_num <= j)
{
switch (nums[mid_num])
{
case 0:
swap(&nums[i++], &nums[mid_num++]);
break;
case 1:
mid_num++;
break;
case 2:
swap(&nums[mid_num], &nums[j--]);
break;
}
}
}
int main()
{
int size;
cout<<"Enter size of array = ";
cin>>size;
int Arr[size];
cout<<"Enter "<<size<<" elements 0s, 1s and 2s only :\n";
for(int i=0;i<size;i++)
{
cin>>Arr[i];
}
cout << "Original array: ";
for (int i=0; i < size; i++)
cout << Arr[i] <<" ";
sort_012_num(Arr, size);
printf("\nArray elements after rearranging: ");
for (int i=0; i < size; i++)
cout << Arr[i] <<" ";
return 0;
}
Output
Question 5
Create a string (using both approaches -C Style string and string class) and carry
out following operations:
-Reverse a string
-Sort a string
-Convert a string into Upper Case
Source Code:
Output
Question 6
Accept a string from he user and print the number of times each character is
appearing in the string.
Source Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char str[100];
int char_counts[256] = {0};
cout<<"Enter a string: ";
cin>>str;
for (int i = 0; str[i] != '\0'; i++) {
char_counts[str[i]]++;
}
cout<<"The number of times each character appears in the string is as follows:\n";
for (int i = 0; i < 256; i++) {
if (char_counts[i] > 0) {
printf("%c: %d\n", i, char_counts[i]);
}
}
return 0;
}
Output
Question 7
Create a C++ program to perform survey on four different model of Maruti
(Marut- K10, Zen-Astelo, Wagnor, Maruti- SX4) owned by person living in four
metro cities (Delhi, Mumbai, Chennai & Kolkata). Display tabulated report like
format given below:
City Maruti- Zen-Astelo Wagnor Maruti-
K10 SX4
Delhi
Mumbai
Chennai
Kolkata
Calculate numbers of cars of different model in each metro city.
Source Code:
#include<iostream>
using namespace std;
int main()
{
int survey[4][4];
int i,j,citycode,carcode;
char choice;
string Cities[4]={"Delhi ","Mumbai ","Kolkata","Chennai"};
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
survey[i][j]=0;
}
}
do
{
cout<<"****Main Menu****"<<endl;
cout<<"_________________\n"<<endl;
cout<<"*******CITY*******"<<endl;
cout<<"Press[0] for Delhi"<<endl;
cout<<"Press[1] for Mumbai"<<endl;
cout<<"Press[2] for Kolkata"<<endl;
cout<<"Press[3] for Chennai"<<endl;
cout<<"Enter City Code = ";
cin>>citycode;
cout<<"\n*******CAR*******"<<endl;
cout<<"\nPress[0] for Alto K10"<<endl;
cout<<"Press[0] for Zen-Astelo"<<endl;
cout<<"Press[0] for WagonR"<<endl;
cout<<"Press[0] for Maruti-SX4"<<endl;
cout<<"Enter Car Code =";
cin>>carcode;
survey[citycode][carcode]++;
cout<<"Do you want to continue (y/n): ";
cin>>choice;
}while(choice=='y');
cout<<"\nCITY | Maruti-K10 Zen-Astelo WagonR Maruti-SX4\n";
cout<<"_________________________________________________________\n";
for(i=0;i<4;i++)
{ cout<<Cities[i]<<"|";
for(j=0;j<4;j++)
{
cout<<" "<<survey[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Output
Question 8
The daily maximum temperatures recorded in 5 cities during the month ofJanuary
(for a week) have been tabulated as follows:
Day Delhi Mumbai Kolkatta Chennai Dehradun
mon
tues
sun
Write a program to read the table elements into a two-dimensional array temperature, and to find
the city and day corresponding to
(a) the highest temperature and
(b) the lowest temperature.
Source Code:
Output
Question 9
Create an inline function which accepts a number and checks whether its
Armstrong or not.
Source Code:
#include<iostream>
using namespace std;
inline bool is_armstrong(int n) {
int sum_of_digits = 0;
while (n > 0) {
int digit = n % 10;
sum_of_digits += digit * digit * digit;
n /= 10;
}
return sum_of_digits == n;
}
int main() {
int n;
cout<<"Enter a number = ";
cin>>n;
if (is_armstrong(n))
{
cout << n << " is an Armstrong number." << endl;
} else {
cout << n << " is not an Armstrong number." << endl;
}
return 0;
}
Output
Question 10
Create a class Room, which includes following members
a) Length
b) Breadth
c) Height
And Methods
a) CalculateArea()
b) CalculateVolume()
c) Display()
Source Code:
#include<iostream>
using namespace std;
class Room {
public:
double length, breadth, height;
double CalculateArea() {
return length * breadth;
}
double CalculateVolume() {
return length * breadth * height;
}
void Display() {
cout << "Length: " << length << endl;
cout << "Breadth: " << breadth << endl;
cout << "Height: " << height << endl;
cout << "Area: " << CalculateArea() << endl;
cout << "Volume: " << CalculateVolume() << endl;
}
};
int main()
{
Room dim;
cout<<"ENTER THE DIMENSIONS OF ROOM\n";
cout<<"L= ";
cin>>dim.length;
cout<<"B = ";
cin>>dim.breadth;
cout<<"H = ";
cin>>dim.height;
cout<<"\n";
dim.Display();
return 0;
}
Output
Question 11
Create a class MyClass. Which includes data members
a) Value
And Methods
a) SetValue() Pass Parameter
b) GetValue() Return Parameter
Source Code:
#include<iostream>
using namespace std;
class MyClass {
private:
int value;
public:
void SetValue(int value) {
this->value = value;
}
int GetValue() {
return this->value;
}
};
int main()
{ int ret_val;
MyClass my_class;
my_class.SetValue(15);
cout<<"Parameter passed to SetValue()= 15\n";
ret_val = my_class.GetValue();
cout<<"The value returned by GetValue = "<<ret_val;
return 0;
}
Output
Question 12
Write a program to implement array of object.
Source Code:
#include <iostream>
using namespace std;
class MyClass {
public:
int value;
MyClass() {
value = 0;
}
MyClass(int v) {
value = v;
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
const int arraySize = 5;
MyClass myArray[arraySize];
// Initialize objects in the array
for (int i = 0; i < arraySize; ++i) {
myArray[i].value = i + 1;
}
// Display values of objects in the array
for (int i = 0; i < arraySize; ++i) {
myArray[i].display();
}
return 0;
}
Output
Question 13
Write a program to count number of objects created for a class using constructor.
Source Code:
#include <iostream>
using namespace std;
class MyClass {
private:
static int objectCount; // Static member variable to count objects
public:
MyClass() {
objectCount++; // Increment the count when an object is created
}
~MyClass() {
objectCount--; // Decrement the count when an object is destroyed
}
static int getObjectCount() {
return objectCount; // Return the count of objects
}
};
// Initialize the static member variable outside the class definition
int MyClass::objectCount = 0;
int main() {
MyClass obj1;
MyClass obj2;
MyClass obj3;
MyClass obj4;
cout << "Number of objects created: " << MyClass::getObjectCount() << endl;
return 0;
}
Output