Harlan A. Brothers and John A. Knox discovered that as the value of x gets large
ID: 3856014 • Letter: H
Question
Harlan A. Brothers and John A. Knox discovered that as the value of x gets larger, the value of the expression ( 2x+1/2x1 ) x gets closer and closer to e. Write a program that evaluates this expression for x = 1, 2, 3 and so on until he absolute difference between the expression’s value and the value of M E defined in math.h is less than 0.000001. Display he value if x that causes your loop to exit along with both the final approximation of e and the value of M E. Show seven decimal places. (in C with COMMENTS)
Explanation / Answer
Code :-
#include <stdio.h>
#include <math.h>
int main(void) {
double x=0.0;
double echeck=0.000001;
const double e= 2.71828182845904524;
double result;
double diff;
do{
x++;
result=(2*x + 1)/(2*x - 1); //expression
if(result > e){ //conditions for absolute difference
diff = result - e;
}
else{
diff = e - result;
}
}while(diff < echeck); //condition for checking whether the result is less than 0.000001
//printing all the values
printf("The value of x = %2.0lf ",x);
printf("The value of expression = %8.7lf ",result);
printf("The value of e =%8.7lf ",e);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.