2. Write a program to find the approximation of In(10) using the following formu
ID: 3590856 • Letter: 2
Question
2. Write a program to find the approximation of In(10) using the following formula for m terms: collo Indl 0)2(9/10- whe where n-1, 2, m Prompt the user to enter the number of terms (between 10 and 50 inclusive) to be used in the approximation. Use a while validation loop to assure that the number of terms is in the proper range. Use a for loop to calculate each term in the sum and while doing so, to print a table. Each line of the table should include the term (value of n) number, the value of the term added to the sum at that iteration accurate to 5 decimal digits, and the value of the sum at that iteration also printed with 5 decimal digits. Use doubles for the term and sum approximation values. At the end of the program print the final value of the sum and also the actual value of In(10) from the C library using 5 decimal digitsExplanation / Answer
#include<stdio.h>
#include<math.h>
int main(){
int m,n;
double sum = 0;
//user input
printf("Enter number:");
scanf("%d",&m);
//get valid number
while(m<10 || m>50){
printf("Invalid number Enter number:");
scanf("%d",&m);
}
for(n=1;n<m+1;n++){
//calculate term value and add it to sum
double term = pow((9/10.0),m)/n;
sum += term;
printf("term number:%d,term value %.5f ",n,term);
}
printf("Approximation::ln(10):%.5f ",sum);
printf("Actual::ln(10):%.5f ",log10(10));
return 0;
}
/*
sample output
Enter number: 10
term number:1,term value 0.34868
term number:2,term value 0.17434
term number:3,term value 0.11623
term number:4,term value 0.08717
term number:5,term value 0.06974
term number:6,term value 0.05811
term number:7,term value 0.04981
term number:8,term value 0.04358
term number:9,term value 0.03874
term number:10,term value 0.03487
Approximation::ln(10):1.02127
Actual::ln(10):1.00000
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.