Optimizing a simple program
Code:
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float totalPaid=0.0, r=0.22, B=5000.0, P=0.0, firstMonth = 0.0, nthMonth = 0.0, n=1.0;
printf("\nEnter your monthly payment: $");
scanf("%f", &P);
printf("\nAnnual Interest Rate (r) = %-5.2f%%\nAmount Borrowed (B) = $%-6.2f\nPayment Amount (P) = $%-6.2f\n", r*100, B, P);
firstMonth = (r/12)*(B);
totalPaid = totalPaid + firstMonth;
printf("\n%-2.0f %-5.2f %-6.2f \n", n, firstMonth, B);
nthMonth=firstMonth;
while(1){
n=n+1;
B = (B-(P-nthMonth));
if (n>2) {
totalPaid = totalPaid + nthMonth;
if (B < 0) { break; } }
else if(n<=2) {
if (B < 0) { break; } }
nthMonth = (r/12)*(B);
printf("%-2.0f %-5.2f %-6.2f \n", n, nthMonth, B); }
printf("\nTotal interest paid: $%-6.2f\n", totalPaid);
exit(0);
}
I already got the code to do exactly what it's needed to do, the only thing that is bothering me is my professor said that this should be a 10-15 line program and mine is double that. Are there any glaringly obvious ways that I can simplify this (while keeping the output the exact same)? I got it down to the point where I don't see anything more that I can optimize.