EDITED FOR CLARITY'S SAKE. I APOLOGIZE FOR THE CONFUSION :3
Okay, so I'm following an online CS class, and we're supposed to write a program, in C, that will tell you how much money you'd have if you had a penny at the beginning of the month and doubled it every day.
Each day you would get double what you had yesterday PLUS everything from the previous days.
Example: You start with .01 and what to calculate a running total by day 3. So the first day is .01, second day is .02, third day is .04. On day 3 you would have 0.01+0.02+0.04 (.09).
The program intends to calculate this process over the duration of any given month (28 - 31 days).
I'm having a really hard time trying to implement this. I've got it doubling it, but I'm not sure how to the previously-calculated days together.
Here's my code:
#include <stdio.h>
#include <math.h>
int main(void) {
/*days represents total days in months*/
/*pens represents the number of pennies on the first day*/
long long days;
long long pens;
do {
printf("Enter the number of days in the month: ");
scanf("%llu", &days);
} while(days < 28 || days > 31);
printf("Enter the initial number of pennies: ");
scanf("%llu", &pens);
for (int i=0; i<= days-1; i++) {
pens += pow(2,i);
printf("You'll have $%llu\n", pens);
}
}
edit2: Okay, so I think I fixed it thanks to all your awesome advice. I changed the last part to:
for (int i=0; i<= days-1; i++)
{
pens = pens + (pens * 2);
}
total = pens / 100;
printf("You'll have $%.2f\n", total);
}
Though there is still a slight issue with the output (which, I'm thinking, is due to the data type I'm using?) It prints out:
You'll have $0.00 You'll have $0.00 You'll have $0.00 You'll have $0.00 You'll have $2.00 You'll have $7.00 You'll have $21.00 You'll have $65.00 You'll have $196.00 You'll have $590.00 You'll have $1771.00 You'll have $5314.00 You'll have $15943.00 You'll have $47829.00 You'll have $143489.00 You'll have $430467.00 You'll have $1291401.00 You'll have $3874204.00
etc.
Pretty good, but I'm betting it's not that accurate since the first few iterations are 0.00.