Lab Report
Lab Report
LAB REPORT
Submitted To-
Submitted By,
#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
int main()
{
int ans;
double mark=0;
cout<< "\t\t\t\t\tQUIZ GAME"<<endl;
cout<< "\t\t\t\t\tARE YOU READY!!!"<<endl;
cout<< "\t\t\t\t\tDev: Hasib Khan"<<endl;
cout<< "Press Enter to Start the Game"<<endl;
getch();
system("cls");
QUIZ GAME
ARE YOU READY!!!
Dev: Hasib Khan
Press Enter to Start the Game
1.
Answer :
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<" Name: MD. Hasibul Islam Khan"<<endl;
cout<<" ID: 12221053"<<endl;
cout<<" Address: Banglabazar Mor,Barishal"<<endl;
}
1. Add, subtract , multiply and divide two numbers.
Answer:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter Two Numbers: "<<endl;
cin>>a>>b;
c=a+b;
cout<<endl<<"Addition: "<<c<<endl;
c=a-b;
cout<<endl<<"Subtraction: "<<c<<endl;
c=a*b;
cout<<endl<<"Multiplication: "<<c<<endl;
c=a/b;
cout<<endl<<"Division: "<<c<<endl;
}
1. Calculate the area of a triangle.
Answer:
#include<bits/stdc++.h>
using namespace std;
int main()
{
double base, height, area;
cin>>base>>height;
area=0.5*base*height;
cout<<" Area= "<<area<<endl;
}
1. Swap two numbers with the third variable.
Answer:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
c=a;
a=b;
b=c;
cout<<a<<" "<<b<<endl;
}
Answer:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<a<<" "<<b<<endl;
}
Answer:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin>>a;
if(a%2==0)
{
cout<< "Even"<<endl;
}
else
{
cout<< "Odd"<<endl;
}
}
1. Print the largest number between 2 numbers.
Answer:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
if (a>=b)
{
cout<<a<<" is the largest number"<<endl;
}
else
{
cout<<b<< " is the largest number"<<endl;
}
}
1. Print the smallest Number among 3 numbers.
Answer:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
if (a<=b && a<=c)
{
cout<<a<<" is the smallest number"<<endl;
}
else if(b<=a && b<=c)
{
cout<<b<< " is the smallest number"<<endl;
}
else
{
cout<<c<< " is the smallest number"<<endl;
}
}
# write a cpp program to check a number is palindrome or not.
#include<iostream>
using namespace std;
int main()
{
int number, temp, reversed=0, rem;
cout<<"Enter any number: ";
cin>>number;
temp=number;
while(number!=0)
{
rem=number%10;
reversed=reversed*10+rem;
number=number/10;
}
if(temp==reversed)
{
cout<<temp<<" is a palindrome";
}
else{
cout<<temp<<" is a not palindrome";
}
return 0;
}
Output:
Enter Any Number: 151
131 is a palindrome
#include <iostream>
using namespace std;
int main() {
int i, n;
bool is_prime = true;
cout << "Enter a positive integer: ";
cin >> n;
if (n == 0 || n == 1) {
is_prime = false;
}
for (i = 2; i <= n/2; ++i) {
if (n % i == 0) {
is_prime = false;
break;
}
}
if (is_prime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number";
return 0;
}
Output:
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
Output:
ans:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i, n, sum = 0;
}
14. Create two classes named Mammals and MarineAnimals. Create another class
named BlueWhale which inherits both the above classes. Now, create a function in each
of these classes which prints "I am mammal", "I am a marine animal" and "I belong to
both the categories: Mammals as well as Marine Animals" respectively. Now, create an
object for each of the above class and try calling
1 - function of Mammals by the object of Mammal
2 - function of MarineAnimal by the object of MarineAnimal
3 - function of BlueWhale by the object of BlueWhale
4 - function of each of its parent by the object of BlueWhale
Ans:-
#include <iostream>
class Mammals {
public:
void mammalFunction() {
std::cout << "I am a mammal" << std::endl;
}
};
class MarineAnimals {
public:
void marineFunction() {
std::cout << "I am a marine animal" << std::endl;
}
};
class BlueWhale : public Mammals, public MarineAnimals {
public:
void bluewhaleFunction() {
std::cout << "I belong to both the categories: Mammals as well as Marine Animals" <<
std::endl;
}
};
int main() {
Mammals mammalObj;
MarineAnimals marineObj;
BlueWhale bluewhaleObj;
mammalObj.mammalFunction();
marineObj.marineFunction();
bluewhaleObj.bluewhaleFunction();
bluewhaleObj.mammalFunction();
bluewhaleObj.marineFunction();
return 0;
}
15. Make a class named Fruit with a data member to calculate the number of fruits in a
basket. Create two other class named Apples and Mangoes to calculate the number of
apples and mangoes in the basket. Print the number of fruits of each type and the total
number of fruits in the basket.
Ans:-
#include <iostream>
class Fruit {
protected:
int numFruits;
public:
Fruit() : numFruits(0) {}
void setNumFruits(int num) {
numFruits = num;
}
int getNumFruits() const {
return numFruits;
}
};
class Apples : public Fruit {
public:
void display() {
std::cout << "Apples: " << getNumFruits() << std::endl;
}
};
class Mangoes : public Fruit {
public:
void display() {
std::cout << "Mangoes: " << getNumFruits() << std::endl;
}
};
int main() {
Apples appleBasket;
Mangoes mangoBasket;
appleBasket.setNumFruits(10);
mangoBasket.setNumFruits(5);
appleBasket.display();
mangoBasket.display();
int totalFruits = appleBasket.getNumFruits() + mangoBasket.getNumFruits();
std::cout << "Total fruits: " << totalFruits << std::endl;
return 0;
}
16. We want to calculate the total marks of each student of a class in Physics,Chemistry
and Mathematics and the average marks of the class. The number of students in the class
are entered by the user. Create a class named Marks with data members for roll number,
name and marks. Create three other classes inheriting the Marks class, namely Physics,
Chemistry and Mathematics, which are used to define marks in individual subject of each
student. Roll number of each student will be generated automatically.
ans:-
#include <iostream>
#include <string>
using namespace std;
class Marks {
protected:
int rollNumber;
string name;
double marks;
public:
Marks(int roll, const string& studentName, double studentMarks)
: rollNumber(roll), name(studentName), marks(studentMarks) {}
void displayDetails() {
cout << "Roll Number: " << rollNumber << endl;
cout << "Name: " << name << endl;
cout << "Marks: " << marks << endl;
}
};
class Physics : public Marks {
public:
Physics(int roll, const string& studentName, double physicsMarks)
: Marks(roll, studentName, physicsMarks) {}
};
class Chemistry : public Marks {
public:
Chemistry(int roll, const string& studentName, double chemistryMarks)
: Marks(roll, studentName, chemistryMarks) {}
};
class Mathematics : public Marks {
public:
Mathematics(int roll, const string& studentName, double mathMarks)
: Marks(roll, studentName, mathMarks) {}
};
int main() {
// Example usage
Physics physicsStudent(101, "Alice", 85.5);
physicsStudent.displayDetails();
return 0;
}
17. We want to store the information of different vehicles. Create a class named Vehicle
with two data member named mileage and price. Create its two subclasses
*Car with data members to store ownership cost, warranty (by years), seating capacity
and fuel type (diesel or petrol).
*Bike with data members to store the number of cylinders, number of gears, cooling
type(air, liquid or oil), wheel type(alloys or spokes) and fuel tank size(in inches)
Make another two subclasses Audi and Ford of Car, each having a data member to store
the model type. Next, make two subclasses Bajaj and TVS, each having a data member to
store the make-type.
Now, store and print the information of an Audi and a Ford car (i.e. model type,
ownership cost, warranty, seating capacity, fuel type, mileage and price.) Do the same
for a Bajaj and a TVS bike.
Ans:-
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
protected:
double mileage;
double price;
public:
Vehicle(double m, double p) : mileage(m), price(p) {}
void displayInfo() {
cout << "Mileage: " << mileage << " km/l" << endl;
cout << "Price: $" << price << endl;
}
};
class Car : public Vehicle {
protected:
double ownershipCost;
int warrantyYears;
int seatingCapacity;
string fuelType;
public:
Car(double m, double p, double cost, int warranty, int seats, const string& fuel)
: Vehicle(m, p), ownershipCost(cost), warrantyYears(warranty),
seatingCapacity(seats), fuelType(fuel) {}
void displayInfo() {
Vehicle::displayInfo();
cout << "Ownership Cost: $" << ownershipCost << endl;
cout << "Warranty (Years): " << warrantyYears << endl;
cout << "Seating Capacity: " << seatingCapacity << " seats" << endl;
cout << "Fuel Type: " << fuelType << endl;
}
};
int main() {
Car audi(15.5, 50000, 2000, 3, 5, "Petrol");
Car ford(12.8, 45000, 1800, 2, 5, "Petrol");
cout << "Audi Car Info:" << endl;
audi.displayInfo();
cout << "\nFord Car Info:" << endl;
ford.displayInfo();
return 0;
}
18. Create a class named Shape with a function that prints "This is a shape". Create
another class named Polygon inheriting the Shape class with the same function that
prints "Polygon is a shape". Create two other classes named Rectangle and Triangle
having the same function which prints "Rectangle is a polygon" and "Triangle is a
polygon" respectively. Again, make another class named Square having the same
function which prints "Square is a rectangle".
Now, try calling the function by the object of each of these classes.
Ans:-
#include <iostream>
class Shape {
public:
void print() {
std::cout << "This is a shape" << std::endl;
}
};
class Polygon : public Shape {
public:
void print() {
std::cout << "Polygon is a shape" << std::endl;
}
};
class Rectangle : public Polygon {
public:
void print() {
std::cout << "Rectangle is a polygon" << std::endl;
}
};
class Triangle : public Polygon {
public:
void print() {
std::cout << "Triangle is a polygon" << std::endl;
}
};
class Square : public Rectangle {
public:
void print() {
std::cout << "Square is a rectangle" << std::endl;
}
};
int main() {
Shape shapeObj;
Polygon polygonObj;
Rectangle rectangleObj;
Triangle triangleObj;
Square squareObj;
shapeObj.print();
polygonObj.print();
rectangleObj.print();
triangleObj.print();
squareObj.print();
return 0;
}
19. All the banks operating in India are controlled by RBI. RBI has set a well defined
guideline (e.g. minimum interest rate, minimum balance allowed, maximum withdrawal
limit etc) which all banks must follow. For example, suppose RBI has set minimum
interest rate applicable to a saving bank account to be 4% annually; however, banks are
free to use 4% interest rate or to set any rates above it.
Write a program to implement bank functionality in the above scenario. Note: Create
few classes namely Customer, Account, RBI (Base Class) and few derived classes (SBI,
ICICI, PNB etc). Assume and implement required member variables and functions in
each class.
Ans:
#include <iostream>
#include <string>
class RBI {
protected:
double minInterestRate;
double minBalance;
double maxWithdrawalLimit;
public:
RBI(double minInterest, double minBal, double maxWithdrawal) :
minInterestRate(minInterest), minBalance(minBal),
maxWithdrawalLimit(maxWithdrawal) {}
void displayGuidelines() {
std::cout << "RBI Guidelines:" << std::endl;
std::cout << "Min. Interest Rate: " << minInterestRate << "%" << std::endl;
std::cout << "Min. Balance: " << minBalance << " INR" << std::endl;
std::cout << "Max. Withdrawal: " << maxWithdrawalLimit << " INR" << std::endl;
}
};
class Customer {
private:
std::string name;
std::string accountNumber;
public:
Customer(std::string _name, std::string _accNum) : name(_name),
accountNumber(_accNum) {}
void displayInfo() {
std::cout << "Customer: " << name << std::endl;
std::cout << "Account No.: " << accountNumber << std::endl;
}
};
class Account : public RBI {
protected:
double balance;
Customer customer;
public:
Account(double minInterest, double minBal, double maxWithdrawal, double
initBalance, std::string custName, std::string accNum)
: RBI(minInterest, minBal, maxWithdrawal), balance(initBalance),
customer(custName, accNum) {}
void displayDetails() {
customer.displayInfo();
std::cout << "Balance: " << balance << " INR" << std::endl;
displayGuidelines();
}
void deposit(double amount) {
balance += amount;
std::cout << amount << " INR deposited successfully." << std::endl;
}
void withdraw(double amount) {
if (amount <= balance && amount <= maxWithdrawalLimit) {
balance -= amount;
std::cout << amount << " INR withdrawn successfully." << std::endl;
} else {
std::cout << "Insufficient balance or exceeded maximum withdrawal limit." <<
std::endl;
}
}
};
int main() {
Account sbiAccount(4.0, 0.0, 50000.0, 10000.0, "John Doe", "SB1001");
sbiAccount.displayDetails();
sbiAccount.deposit(5000.0);
sbiAccount.withdraw(3000.0);
return 0;
}