Home > Article > Backend Development > C program to calculate the sum of cubes of the first n natural numbers?
The sum of the cubes of the first n natural numbers is a program that adds the cubes of all natural numbers. It is the sum of the series 1^3 2^3 ... n^3, which is the sum of cubes of n natural numbers.
Input:6 Output:441
1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 63 = 441
Use a For Loop to increment and cube the number, then take their sum.
#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; }
The above is the detailed content of C program to calculate the sum of cubes of the first n natural numbers?. For more information, please follow other related articles on the PHP Chinese website!