C++ Assignment Codes and Explanations

0
317

Question 1

i. 

#include <iostream>
using namespace std;

int main() {
float principle = 1000; // GHC 1000 is the principal amount
float rate = 25; // 25% interest rate per year
float time = 2; // two years is the duration of the loan

// calculate simple interest
float interest = (principle * rate * time) / 100;

// calculate monthly interest
float monthly_interest = interest / 24; // 24 months in two years

cout << “Monthly interest gained by the loan company: GHC ” << monthly_interest << endl;

return 0;
}

This program first defines three variables to hold the values of the principal amount, interest rate, and duration of the loan. It then calculates the simple interest using the provided formula and stores it in the interest variable. Finally, it calculates the monthly interest by dividing the total interest by the number of months in the loan period and stores it in the monthly_interest variable.

The program then prints the calculated monthly interest using the cout statement.

ii. 

#include <iostream>
using namespace std;

int main() {
float principal = 1000;
float rate = 25;
float time = 2;
float interest = (principal * rate * time) / 100;
float total = principal + interest;

cout << “Total amount paid including the interest charged for two years: GHC ” << total << endl;

return 0;
}

This program sets the principal amount to GHC 1000, the interest rate to 25%, and the time to 2 years. It then calculates the interest using the simple interest formula and adds it to the principal to get the total amount paid. Finally, it prints the total amount paid to the console in GHC currency.

iii.

#include <iostream>
using namespace std;

int main() {
float principal = 1000.0; // principal amount
float rate = 25.0; // interest rate
float time = 2.0; // time in years
float interest = (principal * rate * time) / 100; // simple interest
float total_amount = principal + interest; // total amount to be paid
float monthly_payment = total_amount / (time * 12); // average monthly payment

cout << “Total amount to be paid: GHC ” << total_amount << endl;
cout << “Average monthly payment: GHC ” << monthly_payment << endl;

return 0;
}

The program initializes the principal amount, interest rate, and time (in years) as float variables. It then calculates the simple interest using the formula I = P * R * T / 100. The total amount to be paid is obtained by adding the simple interest to the principal amount. Finally, the average monthly payment is calculated by dividing the total amount by the number of months in the loan period (2 years * 12 months/year).

The program prints out the total amount to be paid and the average monthly payment in Ghana Cedis (GHC).

Read Also: Full video of Shugatiti’s S@x Tape which she shared on Snapchat and later deleted

Question 2

def calculate_bill(package, minutes_used):
if package == ‘A’:
if minutes_used <= 450:
total_bill = 39.99
else:
additional_minutes = minutes_used – 450
total_bill = 39.99 + (additional_minutes * 0.45)
elif package == ‘B’:
if minutes_used <= 900:
total_bill = 59.99
else:
additional_minutes = minutes_used – 900
total_bill = 59.99 + (additional_minutes * 0.40)
elif package == ‘C’:
total_bill = 69.99
else:
print(‘Invalid package selected’)
return None
return total_bill

# Prompt the user for package and minutes used
selected_package = input(‘Which package did you purchase (A, B, or C)? ‘)
minutes_used = int(input(‘How many minutes did you use this month? ‘))

# Calculate the bill and display the result
total_bill = calculate_bill(selected_package, minutes_used)
if total_bill:
print(f’Your total bill for this month is ${total_bill:.2f}.’)

The calculate_bill function takes two arguments: the customer’s selected package (A, B, or C) and the number of minutes they used in that month. It uses a series of if statements to determine the total bill for the customer based on the package they selected and the number of minutes they used. If an invalid package is selected, the function prints an error message and returns None.

The program prompts the user for the package they purchased and the number of minutes they used, and then calls the calculate_bill function with those values. If the function returns a value (i.e., the package was valid), the program displays the total bill for the customer. The :.2f format specifier is used to display the total bill with two decimal places.

LEAVE A REPLY

Please enter your comment!
Please enter your name here