MHB Compound Interest C programming problem

AI Thread Summary
The discussion revolves around a C programming problem focused on calculating compound interest over 20 years for a principal amount of $1000 at an interest rate of 2%. The user initially struggles with their code, particularly with calculating interest for different compounding periods (yearly, half-yearly, monthly, weekly, and daily). Key issues include an incorrect interest rate, not initializing the time variable properly, and miscalculating the amount for the first year. After receiving guidance, the user successfully revises their code to incorporate various compounding frequencies and formats the output for clarity. The final code allows for user input of the principal and interest rate, generating a comprehensive table of interest calculations.
jasonsmith206
Messages
10
Reaction score
0
Hello everyone, i hate that i can't figure this out and I'm just looking for good direction on how to figure out the last bit of my code. I'm first time user of C programming and haven't fully grasp the concepts and tools available to me so be patient if you offer any help. My task is to write a program that will scan a given principle which is 1000.00 for a interest rate of 2% over 20 years.

My code is this so far.
Code:
#include<stdio.h>
#include<math.h>

int main()

{
   int time;
   double principle, rate, amount, interest;

   principle=1000;
   rate=5.0;   amount=principle*pow((1+rate/100),time);

   printf("Year\tYearly\t    Daily\n");
   printf("----\t------\t    ------");

   for(time=1; time<=20; time++){ //yearly compound interest    printf("\t\t\n%d\t%.2lf\n",time, amount);

   amount=principle*pow((1+rate/100),time);
    }

return 0;
}
I spent a few hours getting the 0-20 year just right and the yearly compound interest is nearly complete but my assigment is to not only do the yearly but also half year, monthly, weekly and daily... this is my problem.

I've tried different ways and i keep getting random numbers that just don't work.

the other problem I'm getting is for YEARLY intererest. It won't calculate the full 20 years it does 1000 2 times for year 0 to 1 and then it compounds the interest leaving me with one year short on compound interest. this is a big assignment i know and I'm sorry for asking for help but my teacher refuses to give advice and the T.As suck really bad they have no idea what to do and i can't afford a tutor to teach me. Please teach me!

Jason
 
Last edited by a moderator:
Technology news on Phys.org
jasonsmith206 said:
Hello everyone, i hate that i can't figure this out and I'm just looking for good direction on how to figure out the last bit of my code. I'm first time user of C programming and haven't fully grasp the concepts and tools available to me so be patient if you offer any help. My task is to write a program that will scan a given principle which is 1000.00 for a interest rate of 2% over 20 years.

My code is this so far.
Code:
#include<stdio.h>
#include<math.h>

int main()

{
   int time;
   double principle, rate, amount, interest;

   principle=1000;
   rate=5.0;   amount=principle*pow((1+rate/100),time);

   printf("Year\tYearly\t    Daily\n");
   printf("----\t------\t    ------");

   for(time=1; time<=20; time++){ //yearly compound interest    printf("\t\t\n%d\t%.2lf\n",time, amount);

   amount=principle*pow((1+rate/100),time);
    }

return 0;
}
I spent a few hours getting the 0-20 year just right and the yearly compound interest is nearly complete but my assigment is to not only do the yearly but also half year, monthly, weekly and daily... this is my problem.

I've tried different ways and i keep getting random numbers that just don't work.

the other problem I'm getting is for YEARLY intererest. It won't calculate the full 20 years it does 1000 2 times for year 0 to 1 and then it compounds the interest leaving me with one year short on compound interest. this is a big assignment i know and I'm sorry for asking for help but my teacher refuses to give advice and the T.As suck really bad they have no idea what to do and i can't afford a tutor to teach me. Please teach me!

Jason

Hi Jason! Welcome to MHB! ;)

Let's see what we have:
  1. To calculate for half a year, we need to fill in [M]time = 0.5[/M].
    Currently that's not possible because time is an integer.
    Instead it should be a double.
  2. Your program has a problem for year 0, since you calculate for it without initializing [M]time[/M].
  3. In each iteration of the for-loop we should first calculate the amount, and then print it, instead of the other way around.
    As a consequence, for instance year 20 is missing.
  4. You have a column "Yearly", but isn't it really the "Amount"?
  5. What is the column "Daily" for?
    It doesn't seem to be used.
  6. Shouldn't the interest rate be 2% instead of 5%?
    That's what your problem statement is saying.

So let's revise your program a little bit.
Code:
#include<stdio.h>
#include<math.h>

int main()
{
   double principle, rate, amount, interest, time;

   principle = 1000;
   rate = 2.0;

   printf("Year\tAmount\n");
   printf("----\t------\n");

   for (time = 0; time <= 20; time += 1.0) { //yearly compound interest
      amount = principle * pow((1 + rate / 100), time);
      printf("%.1lf\t%.2lf\n", time, amount);
   }

   return 0;
}

Perhaps we can add more code to find for instance the half-yearly rate?
 
Hey, yea I'm very new at all this so its hard for me to get the formality of it down while balancing my other classes on campus. What i was trying to do was work backwards from yearly to quarterly to monthly ect. This all has to be done column by column and the only advice my TAs have given us is it'll be loops within a loop. I have the "year" to the far left to indicate the actual year the interest will occure. They i have "yearly" for total yearly principle and interest gained that year. Daily has been difficult because i cant' get the algorithum to work for me so i was toying with it before i did the others. Yes the interest rate should all be %2 percent so idk why i had %5 anywhere
 
jasonsmith206 said:
Hey, yea I'm very new at all this so its hard for me to get the formality of it down while balancing my other classes on campus. What i was trying to do was work backwards from yearly to quarterly to monthly ect. This all has to be done column by column and the only advice my TAs have given us is it'll be loops within a loop. I have the "year" to the far left to indicate the actual year the interest will occure. They i have "yearly" for total yearly principle and interest gained that year. Daily has been difficult because i cant' get the algorithum to work for me so i was toying with it before i did the others. Yes the interest rate should all be %2 percent so idk why i had %5 anywhere

If we change [M]time += 1.0[/M] to [M]time += 0.25[/M], we get amounts for every quarter of a year... (Thinking)
 
yea! i just figured out the calculation error i was getting too. It was an unreal mistake for

principle(1+.02/100), time; was giving me all the interest repeated through the loop for 20 years i got that part

now what i was having trouble on was doing the same kind of loop for bi yearly, monthly, weekly and daily.

I can't believe i over looked that for monthly it was set up like principle(1+.02/1200), 12*time;

had no idea the pow function worked as a power, i just used it cause someone said it helped on a yahoo answer

Now all i need to do is line them all up pretty.

Any ideas? do i need to create 4 separate loops or somehow nest them all?
 
I like Serena said:
If we change [M]time += 1.0[/M] to [M]time += 0.25[/M], we get amounts for every quarter of a year... (Thinking)

I finally got it! after a week at hammering at this thing and getting your help to polish it up this is the end result and I'm super happy with it ha! thank you so much :)

#include<stdio.h>
#include<math.h>

int main()
{
int time;
double principle, rate, Yearly, interest, Quarterly, Monthly, Weekly, Daily;

principle =0;
rate =0;

printf("Note: Entering the desired amount and interest rate will give various options from Annually to Daily Interest returns for a period of 20 years\n\n");
printf("Please Enter your Principle Amount:");
scanf("%lf", &principle);
printf("\n");

printf("Please Enter Desire Interest Rate:");
scanf("%lf", &rate);
printf("\n");

printf("Year\tAnnually\tQuarterly\t Monthly\t Weekly\t\t Daily\n");
printf("----\t------\t\t---------\t---------\t--------\t------\n");

for (time = 0; time <= 20; time +=1 ) { //yearly compound interest
Yearly = principle * pow((1 + rate / 100), time);
Quarterly= principle * pow((1 + rate/ 200), 2 * time);
Monthly= principle * pow((1 + rate/ 1200), 12 * time);
Weekly= principle * pow((1+ rate / 5200), 52 * time);
Daily= principle * pow((1 + rate / 36500), 365 * time);

printf("%d\t%.2lf\t\t%.2lf\t\t%.2lf\t\t%.2lf\t\t%.2lf\n", time, Yearly,Quarterly,Monthly, Weekly, Daily);
} return 0;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top