Compound Interest C programming problem

In summary, your program has a problem for year 0 and it doesn'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!
  • #1
jasonsmith206
10
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
  • #2
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?
 
  • #3
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
 
  • #4
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)
 
  • #5
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?
 
  • #6
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;
}
 

FAQ: Compound Interest C programming problem

What is compound interest?

Compound interest is a type of interest calculation where the interest earned is added to the initial principal amount and then the total amount is used to calculate the interest for the next period. This results in the interest earning interest, leading to a faster growth of the investment.

How is compound interest calculated?

The formula for calculating compound interest is A = P(1 + r/n)^(nt), where A is the final amount, P is the principal amount, r is the annual interest rate, n is the number of times the interest is compounded per year, and t is the number of years. This formula takes into account the compounding period and the time period.

What is the difference between simple interest and compound interest?

Simple interest is calculated only on the initial principal amount, whereas compound interest takes into account the interest earned in previous periods, resulting in a larger final amount. This means compound interest leads to higher returns over time compared to simple interest.

How can I implement a compound interest calculation in C programming?

To implement a compound interest calculation in C programming, you will need to declare variables for the principal amount, interest rate, compounding period, and time period. Then, use the formula A = P(1 + r/n)^(nt) to calculate the final amount. You can also use a loop to calculate the interest earned for each period and add it to the principal amount.

What are some real-life examples of compound interest?

Compound interest is commonly seen in savings accounts, where the interest earned is added to the initial deposit, resulting in a larger amount over time. It is also used in investments, such as stocks and mutual funds, where the returns are reinvested to generate even higher returns. Credit card companies also use compound interest to calculate the interest on outstanding balances.

Similar threads

Replies
2
Views
5K
Replies
22
Views
5K
Replies
21
Views
9K
Replies
17
Views
4K
Replies
6
Views
1K
Replies
2
Views
1K
Replies
9
Views
2K
Back
Top