Given a positive integer n
, use a loop to store the sum of the reciprocals of the squares of the first n
natural numbers in a float variable called sum
:
Let's say the value of n
is 4, then the sum would be:
This series is part of the Basel Problem. The infinite sum gives us .
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j+=2) {
statement
}
}
Note: the pattern provided is for n = 5. Your code should work for values of n between 1 and, say, 0.
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Note: the pattern provided is for n = 4 (hence, 4 lines). Your code should work for values of n between 1 and, say, 20.
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
HINT: The circles are along the circumference of a bigger circle, just at different angles.
In the image above screen size of 400 by 400 pixels has been used. Additionally, the circles are offset by its radius.
for (int i = 15; i > 0; i-=5) {
for (int k = 1; k < i; k += 3) {
statement
}
}
for (int x = 20; x < width; x = x + 40)
{
for (int y = 20; y <= x; y = y + 40)
{
circle(x, y, 10);
circle(x, y - height, 10);
}
}
The program is meant to produce the following image:
however, it is instead producing the following instead.
Find & correct for the error.