Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Using a for loop to calculate Pi

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Using a for loop to calculate Pi

    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);
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Using a for loop to calculate Pi

    a way to get the [1/(n^2)] into the code
    Have you tried to write that expression in the code? One simplification would be to replace n^2 with n*n

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. what is the way to Calculate
    By ohad in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 8th, 2013, 11:29 AM
  2. Game Loop, calculate the Frames Per Second.
    By Dirk94 in forum Threads
    Replies: 2
    Last Post: June 20th, 2012, 07:35 AM
  3. Cannot calculate. Please help me....
    By safarina02 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2011, 01:11 PM
  4. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM
  5. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2009, 11:41 PM