Document From SHAMI
Document From SHAMI
Question:
You are tasked with developing a C++ program for a bank. The program should calculate the interest
for a fixed deposit account. The interest is calculated based on the principal amount, rate of interest,
and the duration of the deposit. However, if the rate of interest and the duration are not provided by the
user, the program should use default values.
Create a function that will calculate the interest, where:
● The principal amount is mandatory (must be provided by the user).
● The rate of interest and the duration are optional (the program should use default values if they
are not provided).
Aim:
Algorithm:
#include<iostream>
using namespace std;
float calc_Interest(float principal,float rate,int duration)
{
return (principal*rate*duration)/100;
}
float principal,rate;
int duration;
cout << "Enter principal amount: ";
cin >> principal;
cout << "Enter rate of interest and duration(or press 0 to use default values): ";
cin >> rate >> duration;
float interest = (rate==0 && duration==0) ? calc_Interest(principal) :
calc_Interest(principal,rate,duration);
cout << "Interest: " << interest << endl;
return 0;
}
Result:
Question:
You are developing a utility for a programming course to demonstrate different ways to swap two
integers. The goal is to write a C++ program that swaps the values of two integers in three different
ways:
1. Call by Value: The function should take the values of the integers and attempt to swap them.
2. Call by Address: The function should use pointers to swap the integers by modifying their memory
addresses.
3. Call by Reference: The function should swap the integers by directly accessing them using
references.
Aim:
Algorithm:
#include <iostream>
using namespace std;
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swapByAddress(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void swapByReference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x,y;
cout<<"Enter the values for x and y:";
cin>>x>>y;
cout << "Original values: x = " << x << ", y = " << y << endl;
swapByValue(x, y);
cout << "After swapByValue: x = " << x << ", y = " << y << endl;
swapByAddress(&x, &y);
cout << "After swapByAddress: x = " << x << ", y = " << y << endl;
swapByReference(x, y);
cout << "After swapByReference: x = " << x << ", y = " << y << endl;
return 0;
}
Result:
Question:
You are developing a 3D modeling application that allows users to calculate the volume of various shapes,
including rectangular boxes. To make the calculation process easier for users, the system should
automatically use default dimensions for a box if the user does not provide specific values for its length,
width, and height. Your task is to write a C++ program that calculates the volume of a rectangular box. The
program should allow the user to input the dimensions (length, width, and height) of the box. If any of these
dimensions are not provided by the user, the system should use default values.
Aim:
Algorithm:
#include<iostream>
using namespace std;
int Volume(int length,int width,int height)
{
return length * width * height;
int Volume() {
float def_length = 10, def_width = 8, def_height = 4;
cout << "Using default length=" << def_length << ",width=" << def_width << ",height=" <<
def_height << endl;
return Volume(def_length, def_width, def_height);
int main() {
int length, width, height;
cout << "Enter length,width,height (or press 0 to use default values):";
cin >> length >> width >> height;
int result = (length == 0 && width == 0 && height == 0) ? Volume() : Volume(length, width,
height);
cout << "Volume:" << result << endl;
return 0;
}
Result:
Question:
You are developing a program for a financial application where the system needs to perform division
calculations. The program will divide two numbers, but in some cases, the user might not provide the second
number. If the second number is not provided, the system should default to dividing by 1 to avoid
errors.Your task is to write a C++ program where a function takes pointers to modify the values of the
numbers. If the user provides both numbers, the function should divide them. However, if the second number
is not provided, the function should default to using 1 as the divisor.
Aim:
Algorithm:
#include <iostream>
using namespace std;
void divide(float *num1, float *num2) {
*num1= *num1 / *num2;
int main() {
float num1, num2;
cout << "Enter the first number:";
cin >> num1;
cout << "Enter the second number (or press 0 to use default value 1): ";
cin >> num2;
float result = (num2 == 0) ? divide(num1) : (num1 / num2);
cout << "Result: " << result << endl;
return 0;
Result:
Question:
You are working on a time management application that needs to perform time calculations. One of the key
features of the application is to add two time durations. The times are provided in the format of hours,
minutes, and seconds (hh:mm:ss). Your task is to write a C++ program that will add two time objects and
output the resulting time in the same hh:mm:ss format.
Aim:
Algorithm:
#include<iostream>
using namespace std;
class time
{
int hours,minutes,seconds;
public:
time()
{
hours=0;
minutes=0;
seconds=0;
}
void input()
{
cout<<"Enter the time in (hh:mm:ss):";
cin>>hours>>minutes>>seconds;
}
void add(time t1,time t2)
{
seconds=t1.seconds+t2.seconds;
minutes=t1.minutes+t2.minutes+(seconds/60);
hours=t1.hours+t2.hours+(minutes/60);
seconds%=60;
minutes%=60;
}
void display()
{
cout<<"Total time:"<<hours<<":"<<minutes<<":"<<seconds<<endl;}
}t1,t2,t3;
int main()
{
t1.input();
t2.input();
t3.add(t1,t2);
t3.display();
return 0;
}
Result:
Question:
You are developing a student management system for a school, where the system needs to calculate the
Grade Point Average (GPA) of multiple students. Each student has a set of grades for different subjects, and
you need to calculate their GPA based on these grades. Your task is to develop a program that defines a
Student class with an array as a data member to store the grades of each student.
The program should:
● Accept the number of students (n) from the user.
● Store each student's grades in an array.
● Calculate the GPA based on the grades (you can assume a simple GPA calculation like averaging the
grades).
● Display the GPA for each student.
Aim:
Algorithm:
#include<iostream>
using namespace std;
class Student
{
public:
int grade[10];
int Grades(char grade)
{
if(grade=='A')
{
return 4;
}
if(grade=='B')
{
return 3;
}
if(grade=='C')
{
return 2;
}
if(grade=='D')
{
return 1;
}
if(grade=='F')
{
return 0;
}
return -1;
}
int calc_GPA()
{
int total=0;
for(int i=0;i<5;i++)
{
total+=grade[i];
}
return total/5;
}
};
int main()
{
int n;
cout<<"Enter the number of students:";
cin>>n;
Student students[n]; for(int i=0;i<n;i++)
{
cout<<"Enter the grades for student"<<i+1<<":"<<endl;
for(int j=0;j<5;j++)
{
char grade;
Result:
Question:
You are developing a customer management system for a retail store. The system needs to store customer
information, including their name, using the C++ string class. Additionally, the system should utilize pointers
to manage the name of the customer dynamically. Your task is to develop a program that defines a Customer
class. The class should have a pointer as a data member to store the customer's name dynamically using the
string class. The program should allow the user to input the customer's name and display it when requested.
Aim:
Algorithm:
#include<iostream>
#include<string>
using namespace std;
class Customer
{
public:
string *name; int *age;
string *address;
Customer()
{
name=new string;
age=new int;
address=newstring;
}
~Customer()
{
delete name;
delete age;
delete address;
}
void getdata()
{
cout<<"Enter customer name:";
cin>>*name;cout<<"Enter customer age:";
cin>>*age;cout<<"Enter customer address:";
cin>>*address;
}
void display()const
{
cout<<"Customer Information:"<<endl;
cout<<"Name:"<<*name<<endl;
cout<<"Age:"<<*age<<endl;
cout<<"Address:"<<*address<<endl;
}
};
void CustomerInfo()
{
Customer*customer=new Customer();
customer->getdata();
customer->display();
delete customer;
}
int main()
{
CustomerInfo();
return 0;
}
Result:
Question:
You are tasked with developing a geometry calculator for a math application. One of the features of the
application is to calculate the area and circumference of a circle. The formula for the area of a circle is
π×r2\pi \times r^2π×r2 and for the circumference, it’s 2×π×r2 \times \pi \times r2×π×r, where rrr is the radius
of the circle.
Your task is to develop a program that defines a Circle class. This class should have a constant data member
PI to store the value of pi, and it should have functions to calculate both the area and the circumference of the
circle based on its radius.
The program should:
● Use a constant data member to represent the value of pi.
● Allow the user to input the radius of the circle.
● Compute and display the area and circumference using the appropriate formulas.
Aim:
Algorithm:
#include<iostream>
using namespace std;
class circle
{
const float PI;
float radius;
public:
circle(float r):PI(3.14),radius(r)
{}
float getArea()
{
return PI*radius*radius;
}
float getCircumference()
{
return 2*PI*radius;
}
void display()
{
cout<<"Radius:"<<radius<<endl;
cout<<"Area:"<<getArea()<<endl;
cout<<"Circumference:"<<getCircumference()<<endl;
}
};
int main()
{
float r;
cout<<"Enter the radius of the circle:";
cin>>r;
circle c(r);
c.display();
return 0;
}
Result:
Question:
You are developing a supermarket billing system for a retail store. The system needs to handle the billing of
multiple products purchased by a customer. Each product has a price and a quantity, and the system should
calculate the total cost for the customer, including a final bill with the itemized list of purchased products.
Your task is to develop a program to implement the billing system using classes, member functions, and
constructors.
The program should:
● Store product details such as product name, price, and quantity.
● Calculate the total price for each product and generate the final bill.
● Use constructors to initialize product details when a customer adds items to their cart.
● Display the bill with the total cost for all items.
Aim:
Algorithm:
#include<iostream>
using namespace std;
class Product
{
public:
stringname;
float price;
int quantity;
void input()
{
cout<<"Product Name:";
cin>>name;
cout<<"Price:";
cin>>price;
cout<<"Quantity:";
cin>>quantity;
}
float totalprice()
{
return price*quantity;
}
void display()
{
cout<<name<<"\t"<<price<<"\t\t\t"<<quantity<<"\t\t"<<totalprice()<<endl;
}
};
int main()
{
int numProducts;
float totalcost=0;
Product products[5];
cout<<"Enter the number of Products:";
cin>>numProducts;
for(int i=0;i<numProducts;i++)
{
cout<<"Product:"<<(i+1)<<"\t"<<"details:
\n"; products[i].input();
}
cout<<"\nBill:"<<endl;
cout<<"Name\tPrice\tQuantity\tTotal\n";
for(int i=0;i<numProducts;i++)
{
products[i].display();
totalcost+=products[i].totalprice();
}
cout<<"\n Total Cost:"<<totalcost<<endl;
return 0;
}
Bill:
Name Price Quantity Total
Apple 100 10 1000
Banana 50 10 500
Total Cost:1500
Result: