MNY
MNY

Reputation: 1536

Cant print out my function calculation

My task is to take someones who won a jackpot of 1m dollar, put it in the bank and earns 8% yearly rate on his money. Now, before every year ends he pulls 100k. So how many years it will take him to empty his account?

This is my code:

#include <stdio.h>
long int year_ended(long int prize);//declare the function to be implemented 


int main(void)

{

    long int jackpot, years;

    jackpot = 1000000;

    for (years = 0; jackpot >= 0; years++) //counting the years until the jackpot is 0
        year_ended(jackpot); 

    printf("it will take %ld years to empty the account", years);

    return 0;
}

long int year_ended(long int prize) //function to decrement 100k every year and add the erned interest

{
    double yearlyRate = 0.08;

    int YearlyWithdraws = 100000, left;

    left = (prize - YearlyWithdraws) * yearlyRate + (prize - YearlyWithdraws);

    prize = left;

    return prize;
}

The error is: the program keeps running, but i get no output..

What am i doing wrong?

Upvotes: 2

Views: 61

Answers (1)

P.P
P.P

Reputation: 121357

for (years = 0; jackpot >= 0; years++)
    year_ended(jackpot); 

This is an infinite loop as the condition is always true. You probably want to modify jackpot. Something like:

for (years = 0; jackpot >= 0; years++) 
    jackpot = year_ended(jackpot); 

Upvotes: 3

Related Questions