You are on page 1of 11

Practicals

program 1
Create a program that calculates the volume of a triangular prism
and the area of a cross- section of the same prism. Your program
should ask the user to enter the length of the prism and the height
and base of the triangular cross-section of the prism. Ourput your
results using printf() instead of cout.

Here is a sample run:


Enter the height the triangular prism:
10
Enter the base of the triangular
prism: 5
Enter the length of the triangular
prism: 20
The area of a cross section is:
25.000000
The volume of the triangular prism
is: 500.000000

Code #include <iostream>


using namespace std;
int main()
{
float h,b,l;
printf("Enter the Height of the triangular prism : ");
cin>>h;
printf("Enter the Base of the triangular prism : ");
cin>>b;
printf("Enter the Length of the triangular prism : ");
cin>>l;
printf("Area of the triangular prism is %0.6f\n",0.5*b*h);
printf("Volume of the triangular prism is %0.6f",0.5*b*h*l);
}
output

program 2 When you borrow money to buy a house, a car, or for some other purposes, then
you typically repay it by making periodic payments. Suppose that the loan amount
is L, r is the interest rate per year, m is the number of payments in a year, and the
loan is for t years. Suppose that i = (r / m) and r is in decimal. Then the periodic
payment is:

You can also calculate the unpaid loan balance after making certain payments. For
example, the unpaid balance after making k payments is:

where R is the periodic payment. (Note that if the payments are monthly, then m =
12.) Write a program that prompts the user to input the values of L, r, m, t, and k.
The program then outputs the appropriate values. Your program must contain at
least two functions, with appropriate parameters, to calculate the periodic
payments and the unpaid balance after certain payments. Make the program
menu driven and use a loop so that the user can repeat the program for different
values.

code #include <bits/stdc++.h>


using namespace std;
float PeriodicPayment(float p,int q,int w,int a)
{
float i = p/q,R;
R = (a*i)/(1 - pow((1+i),-q*w));
cout<<pow((1+i),-q*w)<<endl;
cout<<"Periodic Payment : "<<fixed
<<setprecision(2)<<R<<endl;
}
float UnpaidBalance(float e,int f,int h,int b)
{
int k;
float G,i = e/f;//float d = PeriodicPayment(e,f,h,b);
cout<<"Enter the no. of payments made : ";
cin>>k;
G = ((b*i) / (1 - pow((1+i),-f*h)))*(1 - pow((1+i),-(f*h - k)))/i;
cout<<"Unpaid Balance : "<<fixed
<<setprecision(2)<<G;
}
int main()
{
int l,c;
int m,t;
float r;
do{
cout<<"1.Periodic Payment"<<endl;
cout<<"2.Unpaid Balance"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your choice : ";
cin>>c;
if(c == 3){
exit(0);
}
if(c<1 || c>3)
{
cout<<"Invalid Choice";
exit(0);
}
else{
cout<<"Enter the loan amount : ";
cin>>l;
cout<<"Enter the rate of interest : ";
cin>>r;
cout<<"Enter the time in years : ";
cin>>t;
cout<<"Enter the number of payments in a year : ";
cin>>m;
switch(c)
{
case 1:
PeriodicPayment(r,m,t,l);
break;
case 2:
UnpaidBalance(r,m,t,l);
break;
}
}
}while(c != 3);
}
ouput

program 3 Design and implement a class dayType that implements the day of the week in a
program. The class dayType should store the day, such as Sun for Sunday. The
program should be able to perform the following operations on an object of type
dayType:
Set the day.
Print the day.
Return the day.
Return the next day.
Return the previous day.

Calculate and return the day by adding certain days to the current day. For example,
if the current day is Monday and we add 4 days, the day to be returned is Friday.
Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
Add the appropriate constructors.

code #include <bits/stdc++.h>


using namespace std;
class dayType
{
int i,j,l;
string n,week[7];
public:
dayType()
{
week[0] = "Sun";
week[1] = "Mon";
week[2] = "Tue";
week[3] = "Wed";
week[4] = "Thurs";
week[5] = "Fri";
week[6] = "Sat";
}
void print()
{
for(int k=0;k<7;k++)
{
if(week[k]==n)
{
i = k + 1;
cout<<"The day you want to print : "
<<week[k]<<endl;
break;
}
else{continue;}
}
}
void set()
{
cout<<"Enter the day you want to set : ";
cin>>n;
}
string add()
{
cout<<"Enter no. of day/s you want to add : ";
cin>>j;
l = (i + j) % 7;
if(l==0)
return week[6];
else
return week[l-1];
}
string get()
{
return n;
}
string yesterday(string t);
string today(string t);
string tomorrow(string t);
};
string dayType :: today(string t)
{
if(t == "Sun")
return "Sun";
else if(t == "Mon")
return "Mon";
else if(t == "Tue")
return "Tue";
else if(t == "Wed")
return "Wed";
else if(t == "Thurs")
return "Thurs";
else if(t == "Fri")
return "Fri";
else if(t == "Sat")
return "Sat";
}
string dayType :: yesterday(string t)
{
if(t == "Sun")
return "Sat";
else if(t == "Mon")
return "Sun";
else if(t == "Tue")
return "Mon";
else if(t == "Wed")
return "Tue";
else if(t == "Thurs")
return "Wed";
else if(t == "Fri")
return "Thurs";
else if(t == "Sat")
return "Fri";
}
string dayType :: tomorrow(string t)
{
if(t == "Sun")
return "Mon";
else if(t == "Mon")
return "Tue";
else if(t == "Tue")
return "Wed";
else if(t == "Wed")
return "Thurs";
else if(t == "Thurs")
return "Fri";
else if(t == "Fri")
return "Sat";
else if(t == "Sat")
return "Sun";
}
int main()
{
dayType d1;
d1.set();
d1.print();
cout<<"returned day : "<<d1.get()<<endl;
cout<<"Yesterday : "<<d1.yesterday(d1.get())<<endl;
cout<<"Today : "<<d1.today(d1.get())<<endl;
cout<<"Tomorrow : "<<d1.tomorrow(d1.get())<<endl;
cout<< d1.add();
}
output

program 4 Write the definition of a class, swimmingPool, to implement the properties of a


swimming pool. Your class should have the instance variables to store the length (in
feet), width (in feet), depth (in feet), the rate (in gallons per minute) at which the
water is filling the pool, and the rate (in gallons per minute) at which the water is
draining from the pool. Add appropriate constructors to initialize the instance
variables. Also add member functions to do the following: determine the amount of
water needed to fill an empty or partially filled pool; determine the time needed to
completely or partially fill or empty the pool; add or drain water for a specific
amount of time.

code #include <bits/stdc++.h>


using namespace std;
class swimmingPool
{
int i,j,k,l,m,e;
float volume,TotalWater;
public:
swimmingPool()
{
i = 0;
j = 0;
k = 0;
l = 0;
}
void get()
{
cout<<"Enter the Height of the pool "
<<"in feet : ";
cin>>i;
cout<<"Enter the Length of the pool "
<<"in feet : ";
cin>>j;
cout<<"Enter the Width of the pool "
<<"in feet : ";
cin>>k;
cout<<"Enter the rate at which water"
<<" is filling up the pool : ";
cin>>m;
cout<<"Enter the rate at which water"
<<" is draining from the pool : ";
cin>>l;
volume = i*j*k;
TotalWater = volume*7.481;
}
void put();
};
void swimmingPool :: put()
{
cout<<"volume of the pool is "<<volume<<endl;
cout<<"water required for filling empty pool : "<<TotalWater<<endl;
cout<<"Time required for filling up the pool : "
<<fixed<<setprecision(2)<<TotalWater/m<<endl;
cout<<"Enter the no. of gallons by which pool is filled : ";
cin>>e;
cout<<"water required for partially filling the pool : "
<<(TotalWater-e)<<endl;
cout<<"Time required for partially filling the pool : "
<<fixed<<setprecision(2)<<(TotalWater-e)/m<<endl;
cout<<"Time required for emptying the pool : "
<<fixed<<setprecision(2)<<TotalWater/l<<endl;
cout<<"Time required for partially emptying the pool : "
<<fixed<<setprecision(2)<<e/l;
}
int main()
{
swimmingPool s;
s.get();
s.put();
}
output

Program 5 Write a program that converts a number entered in Roman numerals to decimal.
Your program should consist of a class, say, romanType. An object of type
romanType should do the following:
Store the number as a Roman numeral.
Convert and store the number into decimal form.
Print the number as a Roman numeral or decimal number as requested by the user.
The decimal values of the Roman numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1
Test your program using the following Roman numerals: MCXIV, CCCLIX,
MDCLXVI.

code #include <bits/stdc++.h>


using namespace std;
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;

return -1;
}
int romanToDecimal(string& str)
{
int res = 0;
for (int i = 0; i < str.length(); i++)
{
int s1 = value(str[i]);
if (i + 1 < str.length())
{
int s2 = value(str[i + 1]);
if (s1 >= s2)
{
res = res + s1;
}
else
{
res = res + s2 - s1;
i++;
}
}
else
{
res = res + s1;
}
}
return res;
}
int main()
{
string str;
bool i;
cout<<"Enter the Roman Numeral : ";
cin>>str;
cout<<"0 for roman number "
<<"and 1 for integer form : ";
cin>>i;
if(i == 1)
{
cout << "Integer form of Roman Numeral is "
<< romanToDecimal(str);
}
else
{
cout << "Roman Number you entered is : "
<< str;
}

return 0;
}
output

You might also like