Given an int variable n that has already been initialized to a positive value and, in addition, int variables k and total that have already been declared use a while loop to compute the sum of the cubes of the first n whole numbers.

Respuesta :

Answer:

// here is code in C++.

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

   // variables

   int n=5;

   int k,total=0;

   int copy=n;

   // while loop to calculate cubes

   while(n)

   {

       // find cube

       k=n*n*n;

       // find total

       total=total+k;

       // decrease n

       n--;

   }

   // print sum of cubes of first n whole numbers

   cout<<"Sum of cube of first "<<copy<<" numbers is "<<total<<endl;

return 0;

}

Explanation:

Declare three  variables n, k and total. initialize n with 5.In the while loop, calculate cube of n then add it to total.Then decrease the n by 1.While loop run until n becomes 0.After the while loop,total will have the sum of cubes of first n whole numbers.

Output:

Sum of cube of first 5 numbers is 225

ACCESS MORE
EDU ACCESS