Reputation: 194
I'm having trouble figuring this out:
Write a fragment that uses a for statement to set the double variable sum to the value of:
Here's what I tried:
class thing
{
public static void main (String [] args)
{
double sum = 1;
for (int i = 1; i<=25; i++)
{
sum += Math.pow(i,1.0/i) ;
System.out.println(sum);
}
}
}
I know this is wrong because it does not end with the proper calculation of 1.137411462. Any help is appreciated! :)
Upvotes: 2
Views: 1732
Reputation: 2031
To add to the other replies above, that sum must start with 0, the calculation as you described isn't accurate.
The value of 25√25 is 1.137411462, not the sum from 1 to 25, in which case if you start with
int sum = 0;
You end up with the total: 30.85410561309813 which is the correct total that you want.
Upvotes: 2
Reputation: 9862
change sum to zero at start .you are adding additiona 1 to sum.
double sum = 0;
for (int i = 1; i<=25; i++)
{
sum += Math.pow(i,1.0/i) ;
}
System.out.println(sum);
Upvotes: 2