Home  >  Article  >  Backend Development  >  C program to calculate the sum of cubes of the first n natural numbers?

C program to calculate the sum of cubes of the first n natural numbers?

WBOY
WBOYforward
2023-08-31 12:37:061561browse

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

Explanation

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.

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(&ldquo;%d&rdquo;, 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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete