Hello!
I recently wrote a simple program that uses a for loop to do a summation. It sums the first n numbers, where the value of n is inputted by the user (code at the end of page). Pretty simple. This got me thinking about other sums I could do. If you take the sum from 1 to infinity of [1/(n^2)], you get [(pi^2)/6]. So I thought could use this same approach to calculate pi. The larger the n value, the closer the approximation. I am having trouble finding a way to get the [1/(n^2)] into the code though. Could I put it in the modifier? Or maybe inside the for loop itself? I've already written the code that will find pi given [(pi^2)/6], so this should be the last step. Thanks!
import java.util.Scanner;
public class PI3
{
public static void main(String[] args)
{
double num, count, total = 0;
System.out.println("Enter the value of n:");
Scanner Nvalue = new Scanner(System.in);
num = Nvalue.nextDouble();
Nvalue.close();
for(count = 1; count <= num; count++)
{
total = total + count;
}
System.out.println("The sum of the first "+num+" natural numbers: "+total);
}
}