Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7044187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:22:47+00:00 2026-05-28T02:22:47+00:00

this is my first interaction with this site, I have heard good things and

  • 0

this is my first interaction with this site, I have heard good things and I hope I can find the answer that I am looking for. I am learning Java and using the Eclipse IDE in a computer science class at my high school and I came across a problem that neither my teacher or I can solve. Here are the instructions.

"The German Mathematician GottfriedLeibniz developed the follow method to approximate the value of π.

π/4 = 1 – 1/3 + 1/5 – 1/7 + …

Write a program that allows the user to specify the number if iterations used in this approximation and displays the resulting value."

Now the code.

import java.util.Scanner;

public class GottfriedLeibnizPi {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        System.out.print("Enter how many iterations you want to go to: ");
        int iterations = reader.nextInt();
        double pi = 0;

        if (iterations >= 0) {
            for (int count = 0; count <= iterations - 1; count++) {
                System.out.println("pi: " + pi + " count: " + count);        //debug statement to show pi and count before running the code
                if (count % 2 == 0) {
                    pi = pi + (1 / (1 + (2 * count)));        // first to run. starts at pi + 1 and every other loop adds 1/(1+2n)
                } else {
                    pi = pi - (1 / (1 + (2 * count)));        // first to run. starts at pi - 1/3 and every other loop subtracts 1/(1+2n)
                }
                System.out.println("pi: " + pi + " count: " + count + "\n");        //debug statement to show pi and count after running the code
            }
            pi = pi * 4;        //obtains the true value of pi
            System.out.println("The value of pi after " + iterations + " iterations is " + pi);
        } else {
            System.out.println("Please enter a non-negative number");
        }
    }
}

Here is the output with the the debugging statements if I enter five at the prompt.

Enter how many iterations you want to go to: 5
pi: 0.0 count: 0
pi: 1.0 count: 0

pi: 1.0 count: 1
pi: 1.0 count: 1

pi: 1.0 count: 2
pi: 1.0 count: 2

pi: 1.0 count: 3
pi: 1.0 count: 3

pi: 1.0 count: 4
pi: 1.0 count: 4

The value of pi after 5 iterations is 4.0

My math says that answer should be 3.3396… but the math in my loop does not appear to run more than once. I have not found anything on here that is close to my problem, does anyone know what is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:22:48+00:00Added an answer on May 28, 2026 at 2:22 am

    The problem seems to be a roundoff caused by integer division. Try replacing the relevant part of your code with this:

    if(count % 2 == 0){
        pi = pi + (1 / (1 + (2.0 * count)));        // first to run. starts at pi + 1 and every other loop adds 1/(1+2n)
    }
    
    else{
        pi = pi - (1 / (1 + (2.0 * count)));        // first to run. starts at pi - 1/3 and every other loop subtracts 1/(1+2n)
    }
    

    Since “1” in the numerator is an integer, and “(1 + (2*Count))” in the denominator is also an integer, you will get integer division which truncates any remainder from the division. Since the denominator in this case is going to be greater than or equal to 1, 1/(positive integer greater than 1) will always result in 0 in integer division. By adding a decimal to one of the integers Java will treat it as a double and not perform integer division.

    So actually, there is no problem with the execution of the lines. When I run the code with the above changes I get the same answer as given by your teacher.

    The value of pi after 5 iterations is 3.3396825396825403
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this my first shot at this awesome new (to me) programmers site, I hope
Using Dozer to map two objects, I have: /** /* This first class uses
I have a JSON data like this: { hello: { first:firstvalue, second:secondvalue }, hello2:
First , I want to thank all the persons who works for this site,
I have only an elementary understanding of XML Schema. This is basically my first
I have a ASP.Net MVC 2 web site, which can be visited by http://localhost/Admin/ContentMgr/
This is my first day to design UI using WPF. I have looked up
This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo
Saw this piece of code in a Ruby on Rails book. This first one
problem euler #5 i found the solution but i don't know why this first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.