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

i need to write a single program that computes two expressions using a while loo

ID: 3544624 • Letter: I

Question

i need to write a single program that computes two expressions using a while loop and a for loop.Read the value of n from the user and compute the left hand side using the specified loop. Use the formula on the right hand side to test the result. Use the same value of n for computation of all the results and display the results.

(a)  First expression using a while loop

1^3+2^3+3^3+4^3+....+n^3=      (^{n}sum_{i=1}^{i^3})    =((n^2*(n+1)^2)/4)

(b) Second expression using for loop

(1/1*2)+(1/2*3)+....+(1/(n*(n+1)))=      (^{n}sum_{i=1}^{1/(i(i+1))})    = (1-(1/n+1))

Explanation / Answer

    1. #include <stdio.h>
    2. int main(void) {
    3. int n;
    4. printf("Please Enter the value of n : ");
    5. scanf("%d",&n);
    6. printf("%d ",n);
    7. int i=1;
    8. int firstExpressionLHS = 0;
    9. int firstExpressionRHS;
    10. //Calculate LHS using while loop
    11. while(i <= n){
    12. firstExpressionLHS += i*i*i;
    13. i++;
    14. }
    15. //Print the LHS
    16. printf("First Expression LHS using while loop : %d ",firstExpressionLHS);
    17. //Compute RHS using the formula
    18. firstExpressionRHS = n*n*(n+1)*(n+1)/4;
    19. //Print the RHS
    20. printf("First Expression RHS using formula : %d ",firstExpressionRHS);
    21. double secondExpressionLHS = 0.0;
    22. double secondExpressionRHS;
    23. //Calculate LHS using for loop
    24. double j;
    25. for(j=1;j<=n;j++){
    26. secondExpressionLHS = secondExpressionLHS + (1)/(j*(j+1));
    27. }
    28. //Print the LHS
    29. printf("Second Expression LHS using for loop : %f ",secondExpressionLHS);
    30. double k = (double)(n + 1);
    31. //Compute RHS using the formula
    32. secondExpressionRHS = (1-(1/k));
    33. //Print the RHS
    34. printf("Second Expression RHS using formula : %f ",secondExpressionRHS);
    35. return 0;
    36. }
    1. //Sample output
       
       Please Enter the value of n : 13 First Expression LHS using while loop : 8281 First Expression RHS using formula : 8281 Second Expression LHS using for loop : 0.928571 Second Expression RHS using formula : 0.928571

      //Another sample output 
       
       
       Please Enter the value of n : 20 First Expression LHS using while loop : 44100 First Expression RHS using formula : 44100 Second Expression LHS using for loop : 0.952381 Second Expression RHS using formula : 0.952381

  1. #include <stdio.h>
  2. int main(void) {
  3. int n;
  4. printf("Please Enter the value of n : ");
  5. scanf("%d",&n);
  6. printf("%d ",n);
  7. int i=1;
  8. int firstExpressionLHS = 0;
  9. int firstExpressionRHS;
  10. //Calculate LHS using while loop
  11. while(i <= n){
  12. firstExpressionLHS += i*i*i;
  13. i++;
  14. }
  15. //Print the LHS
  16. printf("First Expression LHS using while loop : %d ",firstExpressionLHS);
  17. //Compute RHS using the formula
  18. firstExpressionRHS = n*n*(n+1)*(n+1)/4;
  19. //Print the RHS
  20. printf("First Expression RHS using formula : %d ",firstExpressionRHS);
  21. double secondExpressionLHS = 0.0;
  22. double secondExpressionRHS;
  23. //Calculate LHS using for loop
  24. double j;
  25. for(j=1;j<=n;j++){
  26. secondExpressionLHS = secondExpressionLHS + (1)/(j*(j+1));
  27. }
  28. //Print the LHS
  29. printf("Second Expression LHS using for loop : %f ",secondExpressionLHS);
  30. double k = (double)(n + 1);
  31. //Compute RHS using the formula
  32. secondExpressionRHS = (1-(1/k));
  33. //Print the RHS
  34. printf("Second Expression RHS using formula : %f ",secondExpressionRHS);
  35. return 0;
  36. }
  1. //Sample output
     
     Please Enter the value of n : 13 First Expression LHS using while loop : 8281 First Expression RHS using formula : 8281 Second Expression LHS using for loop : 0.928571 Second Expression RHS using formula : 0.928571

    //Another sample output 
     
     
     Please Enter the value of n : 20 First Expression LHS using while loop : 44100 First Expression RHS using formula : 44100 Second Expression LHS using for loop : 0.952381 Second Expression RHS using formula : 0.952381

  1. #include <stdio.h>
  2. int main(void) {
  3. int n;
  4. printf("Please Enter the value of n : ");
  5. scanf("%d",&n);
  6. printf("%d ",n);
  7. int i=1;
  8. int firstExpressionLHS = 0;
  9. int firstExpressionRHS;
  10. //Calculate LHS using while loop
  11. while(i <= n){
  12. firstExpressionLHS += i*i*i;
  13. i++;
  14. }
  15. //Print the LHS
  16. printf("First Expression LHS using while loop : %d ",firstExpressionLHS);
  17. //Compute RHS using the formula
  18. firstExpressionRHS = n*n*(n+1)*(n+1)/4;
  19. //Print the RHS
  20. printf("First Expression RHS using formula : %d ",firstExpressionRHS);
  21. double secondExpressionLHS = 0.0;
  22. double secondExpressionRHS;
  23. //Calculate LHS using for loop
  24. double j;
  25. for(j=1;j<=n;j++){
  26. secondExpressionLHS = secondExpressionLHS + (1)/(j*(j+1));
  27. }
  28. //Print the LHS
  29. printf("Second Expression LHS using for loop : %f ",secondExpressionLHS);
  30. double k = (double)(n + 1);
  31. //Compute RHS using the formula
  32. secondExpressionRHS = (1-(1/k));
  33. //Print the RHS
  34. printf("Second Expression RHS using formula : %f ",secondExpressionRHS);
  35. return 0;
  36. }
#include <stdio.h> int main(void) { int n; printf("Please Enter the value of n : "); scanf("%d",&n); printf("%d ",n); int i=1; int firstExpressionLHS = 0; int firstExpressionRHS; //Calculate LHS using while loop while(i <= n){ firstExpressionLHS += i*i*i; i++; } //Print the LHS printf("First Expression LHS using while loop : %d ",firstExpressionLHS); //Compute RHS using the formula firstExpressionRHS = n*n*(n+1)*(n+1)/4; //Print the RHS printf("First Expression RHS using formula : %d ",firstExpressionRHS); double secondExpressionLHS = 0.0; double secondExpressionRHS; //Calculate LHS using for loop double j; for(j=1;j<=n;j++){ secondExpressionLHS = secondExpressionLHS + (1)/(j*(j+1)); } //Print the LHS printf("Second Expression LHS using for loop : %f ",secondExpressionLHS); double k = (double)(n + 1); //Compute RHS using the formula secondExpressionRHS = (1-(1/k)); //Print the RHS printf("Second Expression RHS using formula : %f ",secondExpressionRHS); return 0; }
//Sample output
 
 Please Enter the value of n : 13 First Expression LHS using while loop : 8281 First Expression RHS using formula : 8281 Second Expression LHS using for loop : 0.928571 Second Expression RHS using formula : 0.928571

//Another sample output 
 
 
 Please Enter the value of n : 20 First Expression LHS using while loop : 44100 First Expression RHS using formula : 44100 Second Expression LHS using for loop : 0.952381 Second Expression RHS using formula : 0.952381