Hello, I need the code below correct to fix the errors also list below. (JAVA) E
ID: 3709167 • Letter: H
Question
Hello,
I need the code below correct to fix the errors also list below. (JAVA)
Error 1: My spacing is one off at the end of the number after the Pi 3.130481885_ <---- space at end
Error 2: I need a space in between "Display Pi after every how many steps?" and "RESULTS"
Error 3: with inputs -1 100 50
my output:
RESULTS
Final Pi at term 0 = 0.000000000
expected output:
RESULTS
At term 50: Pi = 3.121594653
At term 100: Pi = 3.131592904
Final Pi at term 100 = 3.131592904
error 4: with inputs of 1000 -2 -1 500
My code displays at term 2, 4, 6, ...., 1000.
I only need it display like below.
RESULTS
At term 500: Pi = 3.139592656
At term 1000: Pi = 3.140592654
Final Pi at term 1000 = 3.140592654
CODE that needs to be edited:
import java.util.Scanner;
public class PiApproximator {
public static int userInput(String prompt, Scanner scn) {
System.out.println( prompt );
int value = scn.nextInt(); // Taking the user input
return value;
}
public static void printPi(int terms, int displayCount) {
double pi = 0; //intialising pi to zero
int i;
double den;
den = 1; //intialising denominator to zero
for (i = 1; i <= terms; i++) {
if (i % 2 == 1) { // for adding the fraction 4/denominator
pi = pi + (4 / den);
if (i % displayCount == 0) { // Displaying Pi value if 'i' is multiple of displayCOunt
System.out.printf( "At term " + i + ": Pi = %.9f %n", pi );
}
} else if (i % 2 == 0) { // for subtracting the fraction 4/denominator
pi = pi - (4 / den);
if (i % displayCount == 0) { // Displaying Pi value if 'i' is multiple of displayCount
System.out.printf( "At term " + i + ": Pi = %.9f %n", pi );
}
}
den = den + 2; // Incrementing the denomiator by 2 to get values 1,3,5,7,9....
}
System.out.printf( "Final Pi at term " + (i - 1) + " = %.9f %n", pi );
}
public static void main(String[] args) {
System.out.println( "Program will approximate Pi" );
Scanner sc = new Scanner( System.in ); // Declaring a Scanner variable
// Calling userIput function
int terms = userInput( "Enter the number of terms to use:", sc );
int displayCount = userInput( "Display Pi after every how many steps?", sc );
System.out.println( "RESULTS" );
// Calling printPi function
printPi( terms, displayCount );
}
}
Explanation / Answer
PiApproximator.java
import java.util.Scanner;
public class PiApproximator {
public static int userInput(String prompt, Scanner scn) {
System.out.println( prompt );
int value = scn.nextInt(); // Taking the user input
return value;
}
public static void printPi(int terms, int displayCount) {
double pi = 0; //intialising pi to zero
int i;
double den;
den = 1; //intialising denominator to zero
for (i = 1; i <= terms; i++) {
if (i % 2 == 1) { // for adding the fraction 4/denominator
pi = pi + (4 / den);
if (i % displayCount == 0) { // Displaying Pi value if 'i' is multiple of displayCOunt
System.out.printf( "At term " + i + ": Pi = %.9f%n", pi );
}
} else if (i % 2 == 0) { // for subtracting the fraction 4/denominator
pi = pi - (4 / den);
if (i % displayCount == 0) { // Displaying Pi value if 'i' is multiple of displayCount
System.out.printf( "At term " + i + ": Pi = %.9f%n", pi );
}
}
den = den + 2; // Incrementing the denomiator by 2 to get values 1,3,5,7,9....
}
System.out.printf( "Final Pi at term " + (i - 1) + " = %.9f%n", pi );
}
public static void main(String[] args) {
System.out.println( "Program will approximate Pi" );
Scanner sc = new Scanner( System.in ); // Declaring a Scanner variable
// Calling userIput function
int terms = userInput( "Enter the number of terms to use:", sc );
int displayCount = userInput( "Display Pi after every how many steps? ", sc );
System.out.println( "RESULTS" );
// Calling printPi function
printPi( terms, displayCount );
}
}
Output:
Program will approximate Pi
Enter the number of terms to use:
50
Display Pi after every how many steps?
5
RESULTS
At term 5: Pi = 3.339682540
At term 10: Pi = 3.041839619
At term 15: Pi = 3.208185652
At term 20: Pi = 3.091623807
At term 25: Pi = 3.181576685
At term 30: Pi = 3.108268567
At term 35: Pi = 3.170158257
At term 40: Pi = 3.116596557
At term 45: Pi = 3.163812134
At term 50: Pi = 3.121594653
Final Pi at term 50 = 3.121594653
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.