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
-
- #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 outputPlease 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
- #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 outputPlease 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
- #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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.