Using exponential functions in Java.
The program itself is simple enough. However, I'm clueless as to how I should implement this math problem in this for loop... I do have the program itself done, of course.
Code:
public class PopulationGrowth
{
public static void main(String[] args)
{
double pop = 231;
int popCounter = 1980;
for (popCounter = 1980; popCounter <= 2013; ++popCounter)
{
pop = pop * ???????;
}
System.out.printf("The population in 2013 is approximately: %f million.", pop);
}
}
Question marks should be obvious. :f3:
Basically, I'm supposed to predict the population growth from 1980 to 2013, with a 1.03% increase from a 231 million population.
I know it's 231 * (1.103)33. My question is... how do I write that in code? Since it's a for loop, the exponential value would be the counter, but that's all I got.
In case you're wondering, this assignment is to practice the use of for loops, so doing it like this is pretty much the entire point of the assignment.
EDIT: IT'S 1.0103 AND SHOULD NOT BE INCLUDING 2013. Okay oops.
Re: Using exponential functions in Java.
pop *= 1.03;
?
I fail to understand.
Re: Using exponential functions in Java.
Why does this need a loop?
Your loop parameters should not be inclusive of 2013 as that's 34 years, and it should be 1.0103 not 1.03 Danny, geez.
Re: Using exponential functions in Java.
Quote:
Originally Posted by
Locked
pop *= 1.03;
?
I fail to understand.
I did that at first, but I got a different answer from what I calculated...
I'm probably extremely stupid. ;_;
Quote:
Originally Posted by
JoeTang
Why does this need a loop?
Ask my teacher, she assigned this. :f3:
Re: Using exponential functions in Java.
Quote:
Originally Posted by
JoeTang
Why does this need a loop?
Your loop parameters should not be inclusive of 2013 as that's 34 years, and it should be 1.0103 not 1.03 Danny, geez.
Oh it's a 1.03% increase.
Pffft..
> Why does this need a loop?
> In case you're wondering, this assignment is to practice the use of for loops, so doing it like this is pretty much the entire point of the assignment.
You didn't read either. So shhh
Re: Using exponential functions in Java.
NOTE: I'm a bit of a newbie, but I think I can help.
First of, the pineapple is up with your for loop? Wouldn't that reset the year (popCounter) back to 1980 each time it loops?
As for your real question, you have to multiply the amount the population increases by 1.103 each time it loops, assuming I'm understanding the problem right.
double pop = 231;
int popCounter = 1980;
double percentIncrease = 1.103;
for (popCounter = 1980; popCounter <= 2013; ++popCounter)
{
percentIncrease = percentIncrease * percentIncrease;
}
pop = pop * percentIncrease;
System.out.printf("The population in 2013 is approximately: %f million.", pop);
Re: Using exponential functions in Java.
Quote:
Originally Posted by
JoeTang
Your loop parameters should not be inclusive of 2013 as that's 34 years, and it should be 1.0103 not 1.03 Danny, geez.
HAHA OOPS APPARENTLY I CAN'T MATH.
Thanks, it works now. :|
Re: Using exponential functions in Java.
Quote:
Originally Posted by Assignment
(Round the correct answer to two decimal places.)
Oh, I missed that part.
Too bad she never told us how to do that. :(
Re: Using exponential functions in Java.
Quote:
Originally Posted by
MariaColette
Oh, I missed that part.
Too bad she never told us how to do that. :(
%.2f instead of %f.
(Read up on "printf format")
Re: Using exponential functions in Java.
Quote:
Originally Posted by
SaptaZapta
%.2f instead of %f.
(Read up on "printf format")
Ooh, did not know that was a thing. I was under the impression there was only %d/%f/%s. I'll definitely read up more on that.
Thanks!
Re: Using exponential functions in Java.
I've seen you study some pretty in-depth CS classes, how come you're still on a pretty basic programming level?