Write a single program that computes the value of the following expressions usin
ID: 3675267 • Letter: W
Question
Write a single program that computes the value of the following expressions using loop statements. Read a selection from the user and repeat the process once the selection has been executed. There must be an option for the User to exit the program. If user enters: a) Compute the following: x = 1/1 * 2+1/2 * 3+1/3 * 4+...+1/n * (n + 1)= ^n _i=11/i(i + 1) b) Compute the following: x =1/2^0+1/2^2+1/2^4+...+1/2^n= ^n i=0,2,4,6... 1/2^i c) Compute the following: x = 1 +1/1+1/1 * 2+1/1 * 2 * 3+...+1/n! = ^n i=0 1/i! You might remember from your calculus courses that ^n _i=11/i(i + 1)=1 -1/n + 1 So the answer is no. You must do the summation by using loops and NOT with the short hand derivation above. For this assignment, you will need to know if, if/else, and if/else if/else statements modulus % operations (optional) for loops/while loops/do while loops the pow () function (#include ) Be sure to add comments to your code and a header with your name, id, and assignment number. The output does not have to match the exact style of the scenarios in the next page, but the code must work according to the format set forth in the instructions. Submit your.c file through the Blackboard Learn site. The program you submit should be your own work. Cheating will be reported to the office of academic integrity. Both the copier and copiee will be held equally responsible.Explanation / Answer
#include<stdio.h>
#include<math.h>
int main(){
char c;
int n,i;
while(1){
printf("******************************** ");
printf("a - To find sum(1/i(i+1)) ");
printf("b - To find sum(1/2^i) ");
printf("c - To find sum(1/i!)) ");
printf("E - To exit program ");
float sum = 0;
scanf(" %c",&c);
if(c == 'E'){
printf("Exiting program... ");
printf("******************************** ");
break;
}
else if(c == 'a'){
printf("Enter n: ");
scanf("%d",&n);
for(i=1; i<=n; i++){
sum = sum + (1.0/(i*(i+1)));
}
printf("when n = %d , x= %lf ",n,sum);
}
else if(c == 'b'){
printf("Enter n: ");
scanf("%d",&n);
for(i=0; i<=n; i+=2){
int pow2 = pow(2,i);
sum = sum + (1.0/pow2);
}
printf("when n = %d , x= %lf ",n,sum);
}
else if(c == 'c'){
printf("Enter n: ");
scanf("%d",&n);
int fact = 1; sum = 1; // for i=0
for(i=1; i<=n; i++){
fact = fact*i;
sum = sum + 1.0/fact;
}
printf("when n = %d , x= %lf ",n,sum);
}
else{
printf("** Enter a valid letter ** ");
}
printf("******************************** ");
}
printf("******************************** ");
return 0;
}
/*
output:
:~/Desktop/MyCode$ gcc march7test.c -lm
:~/Desktop/MyCode$ ./a.out
********************************
a - To find sum(1/i(i+1))
b - To find sum(1/2^i)
c - To find sum(1/i!))
E - To exit program
a
Enter n: 5
when n = 5 , x= 0.833333
********************************
********************************
a - To find sum(1/i(i+1))
b - To find sum(1/2^i)
c - To find sum(1/i!))
E - To exit program
b
Enter n: 5
when n = 5 , x= 1.312500
********************************
********************************
a - To find sum(1/i(i+1))
b - To find sum(1/2^i)
c - To find sum(1/i!))
E - To exit program
c
Enter n: 5
when n = 5 , x= 2.716667
********************************
********************************
a - To find sum(1/i(i+1))
b - To find sum(1/2^i)
c - To find sum(1/i!))
E - To exit program
d
** Enter a valid letter **
********************************
********************************
a - To find sum(1/i(i+1))
b - To find sum(1/2^i)
c - To find sum(1/i!))
E - To exit program
E
Exiting program...
********************************
********************************
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.