ICSE Class 10 Computer Applications
Question 23 of 69
Iterative Constructs in Java — Question 23
Back to all questions 23
Question Question 19
Write a program to calculate the value of Pi with the help of the following series:
Pi = (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15) ...
Hint: Use while loop with 100000 iterations.
public class KboatValOfPi
{
public static void main(String args[]) {
double pi = 0.0d, j = 1.0d;
int i = 1;
while(i <= 100000) {
if(i % 2 == 0)
pi -= 4/j;
else
pi += 4/j;
j += 2;
i++;
}
System.out.println("Pi = " + pi);
}
}Output
