Assignment CBCP2103 (Computer Programming)
Assignment CBCP2103 (Computer Programming)
COMPUTER PROGRAMMING
Page
Table of Contents ……………… 1
Page 1 of 7
1.0 QUESTION 1
Write a C program to help a prospective borrower calculate the monthly payment for a loan. The
program also prints the amortization (payoff) table to show the balance of the loan after each
monthly payment.
The program prompts the user for input, as shown in the following example:
Banks and financial institutions use different formulas to calculate the monthly payment for a
loan. For the purpose of this assignment, we use the following simple formula:
NM = (NY * 12)
IM = (IY / 12) / 100
P = (1 + IM)NM
Q = (P / (P – 1))
MP = (PR * IM * Q)
where
NY: Scheduled number of years to amortize the loan
NM: Scheduled number of months for the loan
IY: Interest rate per year (as a percentage)
IM: Interest rate / month (decimal)
PR: Principal (the amount of the loan)
P: The value of (1 + IM)NM
Q: The value of P / (P – 1)
MP: Monthly payment
Because of the approximation used in calculation, the value of the new balance at the end of the
last month may become nonzero. To prevent this, the last payment must be adjusted. It may be
less or greater than the other months. It must be calculated by adding the principal paid to the
interest paid for that month. The new balance at the end of the last month must be zero.
The following example shows the concept. The program has been run for a loan of RM 5000.00
at 11% interest rate for a period of 1 year. The input was
Run your program once with the test data shown in the following table.
Page 2 of 7
Amount Interest Years
Set 1 10,000.00 12 1
Set 2 5,000.00 10 2
Set 3 1,000.00 8 3
#include <stdio.h>
#include <math.h>
int main()
{
double PR; /* Principal */
double IY; /* interest rate/year */
double IM; /* interest rate/month */
double MP; /* Monthly payment */
double OB; /* Old balance */
double IP; /* Interest paid */
double PP; /* Principal paid */
double NB; /* New balance */
Page 3 of 7
double P; /* Value of pow ((1+IM),NM) */
double Q; /* The value of P/(P-1) */
int NY; /* Number of year */
int NM; /* Number of month */
int month;
/* user input */
NM = (NY * 12);
IM = (IY / 12) / 100;
P = pow((1 + IM),NM);
Q = P/(P-1);
MP = (PR * IM * Q);
printf("Number of years:\t\t\t%7d\n",NY);
printf("Number of months:\t\t\t%7d\n",NM);
printf("Monthly payment:\t\t\t%10.2lf\n\n",MP);
OB=PR;
printf("%d\t%10.2lf\t%8.2lf\t%6.2lf\t%8.2lf\t%10.2lf\n",month,OB,MP,IP,PP,NB);
OB = NB;
}
Page 4 of 7
printf("\nTotal amount paid: %.2lf\n\n",NM*MP);
return 0;
}
--------------------------------------------------------------------------------------------------------------------
2.0 QUESTION 2
Write a program to compute the real roots of a quadratic equation (ax2 + bx + c = 0). The roots
can be calculated using the following formulas:
− b + b 2 − 4ac − b − b 2 − 4ac
x1 = and x2 =
2a 2a
Your program is to prompt the user to enter the constants (a, b, c). It is then to display the roots
based on the following rules:
a) If both a and b are zero, there is no solution
b) If a is zero, there is only one root (-c / b)
c) If the discriminate (b2 – 4ac) is negative, there are no real roots
d) For all other combinations, there are two roots
a b c
3 8 5
-6 7 8
0 9 -10
0 0 11
--------------------------------------------------------------------------------------------------------------------
2.1 ANSWER
/* Question 2: a program to compute the real roots of a quadratic equation (ax2 + bx + c = 0) */
#include <stdio.h>
#include <math.h>
int main()
{
double a,b,c;
double d;
double x1,x2;
printf("\n");
if(a==0 && b==0)
printf("No solution\n");
else if(a==0)
{
else
{
d= (b*b)-(4*a*c);
if(d<0)
{
printf("There are no real roots\n");
}
else
{
x1=(((-1)*b)+sqrt(d))/(2*a);
x2=(((-1)*b)-sqrt(d))/(2*a);
printf("There are two roots %.2lf, %.2lf\n",x1,x2);
}
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------
ASSIGNMENT ENDS HERE
Page 6 of 7
3.0 Appendixes
3.1 References
• Roberge, J (1995). Data Structure in C++ A Laboratory Course. UK: D.C. Heath and
Company.
• Keong, W.C (2000). Data Structure With C. Kuala Lumpur: Sejana Publishing.
• Bakar, M.A. et al. (2009). CBCP2103 Computer Programming. Selangor: Pearson
Prentice Hall.
• Cprogramming.com.Your resource for C and C++ (”n.d.“). [Online]. Available:
http://www.cprogramming.com/. [2009, Nov,13].
Page 7 of 7