28.2k views
1 vote
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared , use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. use no variables other than n, k, and total.

User Clive
by
8.5k points

1 Answer

5 votes
You don't even need k ...
int n=4;
int total=0;

for(;n>0;n--) { total += n * n * n;}
User SUDO Los Angeles
by
7.7k points