Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Due March 6, 2016 11:59PM (100 pts) Write a single program that computes the val

ID: 3675482 • Letter: D

Question

Due March 6, 2016 11:59PM (100 pts) 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 = + + + += 3+4 1+2 2+3 n*(n + 1) i(i+1) b) Compute the following 20 22 24 2n c) Compute the following: x=1+ + 1 + + + 1+2 1+2+3 n! 1 E) Exit the program You might remember from your calculus courses that 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 amath.h>) Be sure to add comments to your code and a header with your name, id, and assignment mamber The output does not have to match the exact style of the scenarios in the next page, bur the code must work according to the format set forth in the instructions Submit your e file through the BlackBoard Leara site

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 series.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...
********************************
********************************

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote