UNIVERSITY OF CHITTAGONG
ASSIGNMENT ON
Computer Programming (II)
(Part -B)
COURSE NO.-MATH 211
PREPARED FOR
Rajib Karmaker
Assistant Professor,Department of
Mathematics
University of Chittagong
Prepared By
Soraya Yesmin
ID: 22203018
SESSION: 21-22
DATE: 6.3.2025
Department of Mathematics
University of Chittagong
Computer Programming (II)
Part -B
(1)BANK ACCOUNT MANAGEMENT
#include <iostream>
using namespace std;
class BankAccount {
private:
float balance;
public:
BankAccount(float initial_balance) : balance(initial_balance) {}
void deposit(float amount) { balance += amount; }
void withdraw(float amount) { balance -= amount; }
void check_balance() { cout << "Balance: " << balance << endl; }
};
int main() {
BankAccount account(2000);
account.deposit(1050);
account.withdraw(300);
account.check_balance();
return 0;
}
(2)Student ID System
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int id;
public:
Student(string student_name, int student_id) : name(student_name), id(student_id) {}
void display() { cout << "Name: " << name << ", Id: " << id << endl; }
};
int main() {
Student student("Soraya Yesmin", 22203018);
student.display();
return 0;
}
(3)GRADE POINT AVERAGE (GPA) CALCULATION
#include <iostream>
#include <vector>
using namespace std;
class Student {
private:
vector<float> grades;
vector<int> credits;
public:
void add_course(float grade, int credit) {
grades.push_back(grade);
credits.push_back(credit);
}
float calculate_gpa() {
float total_grades = 0, total_credits = 0;
for (int i = 0; i < grades.size(); i++) {
total_grades += grades[i] * credits[i];
total_credits += credits[i];
}
return total_grades / total_credits;
}
};
int main() {
Student student;
student.add_course(3.7, 5);
student.add_course(3.8, 2);
cout << "GPA: " << student.calculate_gpa() << endl;
return 0;
}
(4) LIBRARY MANAGEMENT SYSTEM
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Book {
private:
string title;
bool is_checked_out;
public:
Book(string book_title) : title(book_title), is_checked_out(false) {}
void checkout() { is_checked_out = true; }
void return_book() { is_checked_out = false; }
void display_status() {
cout << "Book: " << title << ", " << (is_checked_out ? "Checked out" : "Available") <<
endl;
}
};
int main() {
vector<Book> books;
books.push_back(Book("C++ Programming"));
books.push_back(Book("Mathematics 204"));
books[0].checkout();
books[0].display_status();
books[1].display_status();
return 0;
}
(5) SIMPLE CALCULATOR
#include <iostream>
using namespace std;
class Calculator {
public:
float add(float a, float b) { return a + b; }
float subtract(float a, float b) { return a - b; }
float multiply(float a, float b) { return a * b; }
float divide(float a, float b) { return a / b; }
};
int main() {
Calculator calc;
cout << "Addition: " << calc.add(5, 3) << endl;
cout << "Subtraction: " << calc.subtract(5, 3) << endl;
cout << "Multiplication: " << calc.multiply(5, 3) << endl;
cout << "Division: " << calc.divide(5, 3) << endl;
return 0;
}
(6) TICKET RESERVATION SYSTEM
#include <iostream>
#include <vector>
using namespace std;
class Ticket {
private:
string event_name;
bool is_reserved;
public:
Ticket(string name) : event_name(name), is_reserved(false) {}
void reserve() { is_reserved = true; }
void cancel() { is_reserved = false; }
void display_status() {
cout << event_name << ": " << (is_reserved ? "Reserved" : "Available") << endl;
}
};
int main() {
Ticket ticket1("Concert");
Ticket ticket2("Train Trip");
ticket1.reserve();
ticket1.display_status();
ticket2.display_status();
return 0;
}
(7) EMPLOYEE SALARY SYSTEM
#include <iostream>
using namespace std;
class Employee {
private:
float hourly_rate;
float hours_worked;
public:
Employee(float rate, float hours) : hourly_rate(rate), hours_worked(hours) {}
float calculate_salary() {
return hourly_rate * hours_worked;
}
};
int main() {
Employee emp(20.5, 40);
cout << "Salary: " << emp.calculate_salary() << endl;
return 0;
}
(8) ONLINE SHOPPING CART
#include <iostream>
#include <vector>
using namespace std;
class Product {
private:
string name;
float price;
public:
Product(string product_name, float product_price) : name(product_name),
price(product_price) {}
float get_price() { return price; }
void display() { cout << name << ": $" << price << endl; }
};
class ShoppingCart {
private:
vector<Product> items;
public:
void add_item(Product product) { items.push_back(product); }
float calculate_total() {
float total = 0;
for (Product item : items) {
total += item.get_price();
}
return total;
}
};
int main() {
ShoppingCart cart;
cart.add_item(Product("Laptop", 799.99));
cart.add_item(Product("Phone", 599.99));
cout << "Total: $" << cart.calculate_total() << endl;
return 0;
}
(9) TIME TRACKING FOR PROJECTS
#include <iostream>
#include <vector>
using namespace std;
class Project {
private:
string name;
float hours_spent;
public:
Project(string project_name) : name(project_name), hours_spent(0) {}
void add_hours(float hours) { hours_spent += hours; }
float get_hours() { return hours_spent; }
void display() { cout << "Project: " << name << ", Hours: " << hours_spent << endl; }
};
int main() {
Project project1("Math Analysis");
project1.add_hours(5);
project1.display();
return 0;
}
(10) INVENTORY MANAGEMENT SYSTEM
#include <iostream>
#include <vector>
using namespace std;
class Item {
private:
string name;
int quantity;
public:
Item(string item_name, int item_quantity) : name(item_name), quantity(item_quantity)
{}
void add_stock(int amount) { quantity += amount; }
void sell(int amount) { quantity -= amount; }
void display() { cout << name << ": " << quantity << " in stock." << endl; }
};
int main() {
Item item1("Widget", 100);
item1.sell(5);
item1.add_stock(10);
item1.display();
return 0;
}
ADVANCED PROBLEMS
(31) Area of Circle: Calculate the area of a circle given its radius.
#include<iostream>
using namespace std;
int main()
{
double radius;
cout<<"Enter a Radius:";
cin>>radius;
double area;
area=radius*radius*3.1416;
cout<<"The Area is " <<area<<endl;
return 0;
}
(32) Area of Triangle: Calculate the area of a triangle given its base and
height.
#include <iostream>
using namespace std;
int main() {
double base, height;
cout << "Enter base and height of the triangle: ";
cin >> base >> height;
double area = 0.5 * base * height;
cout << "Area of the triangle: " << area << endl;
return 0;
}
(33) Area of Rectangle: Calculate the area of a rectangle given its length and
width.
#include <iostream>
using namespace std;
int main() {
double length, width;
cout << "Enter length and width of the rectangle: ";
cin >> length >> width;
double area = length * width;
cout << "Area of the rectangle: " << area << endl;
return 0;
}
(34) Pythagorean Theorem: Calculate the hypotenuse of a right triangle given
the other two sides.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b;
cout << "Enter the lengths of the two other sides of the right triangle: ";
cin >> a >> b;
double hypotenuse = sqrt(a * a + b * b);
cout << "Hypotenuse: " << hypotenuse << endl;
return 0;
}
(35) Simple Interest Calculation: Calculate simple interest given principal,
rate, and time.
#include <iostream>
using namespace std;
int main() {
double principal, rate, time;
cout << "Enter principal, rate, and time: ";
cin >> principal >> rate >> time;
double simpleInterest = (principal * rate * time) / 100;
cout << "Simple Interest: " << simpleInterest << endl;
return 0;
}
(36) Compound Interest Calculation: Calculate compound interest given
principal, rate, and time.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double principal, rate, time;
cout << "Enter principal, rate, and time: ";
cin >> principal >> rate >> time;
double compoundInterest = principal * pow((1 + rate / 100), time) - principal;
cout << "Compound Interest: " << compoundInterest << endl;
return 0;
}
(37) Distance Between Two Points: Calculate the distance between two points
in a Cartesian plane.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x1, y1, x2, y2;
cout << "Enter coordinates of the two points (x1, y1) and (x2, y2): ";
cin >> x1 >> y1 >> x2 >> y2;
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
cout << "Distance: " << distance << endl;
return 0;
}
(38) Sum of an Arithmetic Series: Calculate the sum of an arithmetic series.
#include <iostream>
using namespace std;
int main() {
int firstTerm, commonDifference, n;
cout << "Enter first term, common difference, and number of terms: ";
cin >> firstTerm >> commonDifference >> n;
int sum = n * (firstTerm + (firstTerm + (n - 1) * commonDifference)) / 2;
cout << "Sum of the arithmetic series: " << sum << endl;
return 0;
}
(39) Sum of a Geometric Series: Calculate the sum of a geometric series.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double firstTerm, commonRatio, n;
cout << "Enter first term, common ratio, and number of terms: ";
cin >> firstTerm >> commonRatio >> n;
double sum = firstTerm * (1 - pow(commonRatio, n)) / (1 - commonRatio);
cout << "Sum of the geometric series: " << sum << endl;
return 0;
}
(40) Find the Minimum and Maximum in an Array: Find the minimum and
maximum values in an array.
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int minVal = arr[0], maxVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < minVal) minVal = arr[i];
if (arr[i] > maxVal) maxVal = arr[i];
}
cout << "Minimum value: " << minVal << endl;
cout << "Maximum value: " << maxVal << endl;
return 0;
}
CHALLENGING PROBLEMS
(41) Matrix Determinant: Calculate the determinant of a 2x2 or 3x3 matrix.
#include <iostream>
using namespace std;
// Function to calculate determinant of a 2x2 matrix
int determinant2x2(int matrix[2][2]) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
// Function to calculate determinant of a 3x3 matrix
int determinant3x3(int matrix[3][3]) {
return matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) -
matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) +
matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
}
int main() {
int n;
cout << "Enter size of matrix (2 for 2x2, 3 for 3x3): ";
cin >> n;
if (n == 2) {
int matrix[2][2];
cout << "Enter elements of 2x2 matrix: ";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cin >> matrix[i][j];
}
}
cout << "Determinant: " << determinant2x2(matrix) << endl;
} else if (n == 3) {
int matrix[3][3];
cout << "Enter elements of 3x3 matrix: ";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> matrix[i][j];
}
}
cout << "Determinant: " << determinant3x3(matrix) << endl;
} else {
cout << "Invalid matrix size!" << endl;
}
return 0;
}
(42) Roots of a Cubic Equation: Find the roots of a cubic equation.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c, d;
cout << "Enter coefficients a, b, c, d for the cubic equation: ";
cin >> a >> b >> c >> d;
// Calculate discriminant (simplified)
double delta0 = b * b - 3 * a * c;
double delta1 = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d;
double discriminant = delta1 * delta1 - 4 * delta0 * delta0 * delta0;
cout << "Roots can be found using a numerical method." << endl;
return 0;
}
(43) Binomial Coefficient: Calculate the binomial coefficient
C(n,k)C(n,k)C(n,k).
#include <iostream>
using namespace std;
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
int main() {
int n, k;
cout << "Enter n and k: ";
cin >> n >> k;
int result = factorial(n) / (factorial(k) * factorial(n - k));
cout << "Binomial coefficient: " << result << endl;
return 0;
}
(44) Sieve of Eratosthenes: Implement the Sieve of Eratosthenes to find all
prime numbers up to a given limit.
#include <iostream>
#include <vector>
using namespace std;
void sieve(int limit) {
vector<bool> primes(limit + 1, true);
primes[0] = primes[1] = false;
for (int i = 2; i * i <= limit; i++) {
if (primes[i]) {
for (int j = i * i; j <= limit; j += i) {
primes[j] = false;
}
}
}
cout << "Prime numbers: ";
for (int i = 2; i <= limit; i++) {
if (primes[i]) cout << i << " ";
}
cout << endl;
}
int main() {
int limit;
cout << "Enter limit: ";
cin >> limit;
sieve(limit);
return 0;
}
(45) N-th Fibonacci Number (Recursion): Compute the N-th Fibonacci
number using recursion.
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
cout << "Enter position for Fibonacci number: ";
cin >> n;
cout << "Fibonacci number: " << fibonacci(n) << endl;
return 0;
}
(46) Find the Mode of a Set of Numbers: Calculate the mode of a list of
numbers.
#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
unordered_map<int, int> freq;
for (int i = 0; i < n; i++) {
freq[arr[i]]++;
}
int mode = arr[0], maxCount = 0;
for (auto &entry : freq) {
if (entry.second > maxCount) {
mode = entry.first;
maxCount = entry.second;
}
}
cout << "Mode: " << mode << endl;
return 0;
}
(47) Check Perfect Number: Check if a given number is a perfect number.
#include <iostream>
using namespace std;
bool isPerfectNumber(int num) {
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) sum += i;
}
return sum == num;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPerfectNumber(num)) {
cout << num << " is a perfect number." << endl;
} else {
cout << num << " is not a perfect number." << endl;
}
return 0;
}
(48) Count Prime Numbers in a Range: Count the number of prime numbers
within a specified range.
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
int main() {
int start, end, count = 0;
cout << "Enter range (start and end): ";
cin >> start >> end;
for (int i = start; i <= end; i++) {
if (isPrime(i)) count++;
}
cout << "Prime count: " << count << endl;
return 0;
}
(49) Generate Pascal's Triangle: Print Pascal's Triangle up to a given number
of rows.
#include <iostream>
using namespace std;
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
int binomialCoefficient(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
int main() {
int rows;
cout << "Enter number of rows for Pascal's Triangle: ";
cin >> rows;
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
cout << binomialCoefficient(i, j) << " ";
}
cout << endl;
}
return 0;
}
(50) Binary Search Algorithm: Implement the binary search algorithm to find
an element in a sorted array.
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
int main() {
int size, target;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
cout << "Enter sorted array elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
cout << "Enter the element to search for: ";
cin >> target;
int result = binarySearch(arr, size, target);
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found." << endl;
}
return 0;}