Question

Need this in C programming please with correct output of the code and also please attach...

Need this in C programming please with correct output of the code and also please attach the screenshot of it

// In this problem, we try to find and fix a bug in the following code.

// The following code is trying to calculate a sum that will converge to PI(3.141592....)
// when the number of items goes to infinity.

// This following code is based on the fact that
// that sum of items 8/((4*i +1)*(4*i +3)) for i = 0, 1, 2, ..., up to infinity is PI.

// Find the bug, think through why the bug occurs.
// Fix the bug and submit your code.

// To know more detail about the sum, google the following
// Leibniz formula for π

#include <stdio.h>

int main()
{
   float sum;
   int i, n= 10000;
  
   // Calculating the sum using a for loop
   sum = 0;
   for(i=0; i<=n; i++)
   {
       sum += 8/(4*i + 1)/(4*i + 3);
   }
   //print out the sum.
   //Note how we set the format to only two digits after the decimal point
   printf("The sum is %.2f\n", sum);
   return 0;
}

Homework Answers

Answer #1
#include <stdio.h>

int main()
{
   float sum;
   int i, n= 10000;
  
   // Calculating the sum using a for loop
   sum = 0;
   for(i=0; i<=n; i++)
   {
       sum += 8.0/((4*i+1)*(4*i+3));
   }
   //print out the sum.
   //Note how we set the format to only two digits after the decimal point
   printf("The sum is %.2f\n", sum);
   return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I need to add two trivial functions to the following code. I am having an issue...
I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function Here are some examples of what i can add to the code 1. Obtain opening (current) balance. 2. Obtain number the number of deposits. 3. Obtain number of withdrawals. 4. Obtain deposit amounts. 5. Obtain withdrawal amounts....
could someone please explain this code and the output to me? #include <stdio.h> #include <unistd.h> int...
could someone please explain this code and the output to me? #include <stdio.h> #include <unistd.h> int main(void) { printf("one\n"); fork(); printf("two\n"); return 0; }
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8 13 21 34 55 ....... and also need add code complexity time if you want! here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[ n + 1 ]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for ( i = 2; i < n; i++ ) { fib[ i ] = fib[ i -...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
We see that this computes the product of two matrices. Add a new kernel code, called...
We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices. #include <stdio.h> #include <math.h> #include <sys/time.h> #define TILE_WIDTH 2 #define WIDTH 6 // Kernel function execute by the device (GPU) __global__ void product (float *d_a, float *d_b, float *d_c, const int n) {    int col = blockIdx.x * blockDim.x + threadIdx.x ;    int row = blockIdx.y * blockDim.y + threadIdx.y ;    float...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
C Programming I am trying to also print the frequency and the occurrence of an input...
C Programming I am trying to also print the frequency and the occurrence of an input text file. I got the occurrence to but cant get the frequency. Formula for frequency is "Occurrence / total input count", this is the percentage of occurrence. Can someone please help me to get the frequency to work. Code: int freq[26] = {0}; fgets(input1, 10000, (FILE*)MyFile); for(i=0; i< strlen(input); i++) { freq[input[i]-'a']++; count++; } printf("Text count = %d", count); printf("\n"); printf("Frequency of plain text\n");...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
debug code on C(not C+ or C++) I try to fix by myself, node part show...
debug code on C(not C+ or C++) I try to fix by myself, node part show error. This program should work for scan 5 ints from the user Using those 5 ints, it require construct a linked list of 5 elements. After this, it sould prntf of the list using the PrintList. #include <stdio.h> struct Node{    int data;    Node* next; }; int main(void){    struct Node first = {0, 0};    struct Node* second = {0, 0};   ...
In assembler code must be a gcd.s file So here is the C code I have...
In assembler code must be a gcd.s file So here is the C code I have written originally for the program. My question is how do I convert it to assembly code? //C code #include <stdio.h> int fibonacci(int n) { if(n <= 2) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } } int main(void) { int n; printf("Enter Fibonacci term: "); scanf("%d", &n); printf("The %dth Fibonacci number is %d\n", n, fibonacci(n)); return 0; } Instructions In C...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT