C Program for the cube sum of first n natural numbers?

CServer Side ProgrammingProgramming

The cube sum of first n natural numbers is the program to add cubes of all natural numbers up to n. It is sum of series 1^3 + 2^3 + …. + n^3 that is sum of cube of n natural numbers.

Input:6
Output:441

Explanation

1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 63 = 441

Using For loop to increase the no. and cubing it and taking sum of them.

Example

#include <iostream>
using namespace std;
int main() {
   int n = 6;
   int sum = 0;
   for (int i = 1; i <= n; i++) {
      sum += i * i * i;
   }
   printf(“%d”, sum);
   return 0;
}
raja
Published on 19-Aug-2019 11:40:22
Advertisements