In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf())
Complete the following functions using C programming language:
Calculate the value of π from the infinite series:
π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + …
Complete the double Q4(intQ4_input) function which reads a positive integer Q4_input as an input parameter and calculates the value of π by adding up the first Q4_input terms of the above series. You must return the calculated value as a double
#include<stdio.h> double Q4(int Q4_input){ int i, d = 1; double result = 0; for(i=0;i<Q4_input;i++){ if(i%2==0){ result = result + (1.0/d); } else{ result = result - (1.0/d); } d += 2; } return result*4; } int main(){ int n; printf("Enter input: "); scanf("%d",&n); printf("Result: %lf\n",Q4(n)); return 0; }
Get Answers For Free
Most questions answered within 1 hours.