I need help writing a C program that adds up a given number (typed in by the end user) of terms of the following series: 1/2+1/4+1/8+1/16+…
Sample run 1:
Please type in the number of terms that you want to add in the series 1/2+1/4+1/8+…
3 (typed by end user)
the sum of the first 3 terms = .875
Sample run 2:
Please type in the number of terms that you want to add in the series 1/2+1/4+1/8+…
2 (typed by end user)
the sum of the first 2 terms = .75
You may use the following code as a beginning:
// Programmer: Your name
/* Program purpose:
This program reads in ….. */
#include <stdio.h>
int main( void )
{
int numterms, count=1;
float term, sum = 0,denom = 1;
printf( “Please type in the number of terms that you want to add in the series 1/2+1/4+1/8+ n”);
scanf (“%d”, &numterms);
while ( count++ <= numterms)
{
/* calculate the next power of two */
denom *= 2;
// more code to be added here to complete the assignment
} /* end while */
printf( “The sum of the first %d terms = %fn”, numterms, sum);
return 0; /* indicate successful termination */
} /* end main */
Expert Answer
Dear Student,
here i have completed the given C program as per the requirement.
—————————————————————————————————————————————-
Note: Please note that the below program has been tested on ubuntu 16.04 system and compiled under gcc compiler. This code will also work on other IDE’s.
——————————————————————————————————————————————
Program:
—————————————————————————————————————————————–
//header file declration
#include <stdio.h>
//start of main function
int main( void )
{
//variable declration
int numterms, count=1;
float term, sum = 0, numerator = 1, denom = 1;
//print message
printf( “Please type in the number of terms that you want to add in the series 1/2+1/4+1/8+.. n”);
//scan numberterms
scanf (“%d”, &numterms);
//count until the count <= numterms
while ( count <= numterms)
{
/* calculate the next power of two */
denom *= 2;
//calculate the sum;
sum = sum + numerator/denom;
//increment count
count++;
} /* end while */
printf( “The sum of the first %d terms = %fn”, numterms, sum);
return 0; /* indicate successful termination */
} /* end main */
—————————————————————————————————————————————–
here i have attached multiple sample run of the program as a singlescreen shot…
————————————————————————————————————————————————–
Output: