Home > Backend Development > C++ > 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
Release: 2023-08-31 12:37:06
forward
1606 people have browsed it

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
Copy after login

Explanation

1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 63 = 441
Copy after login

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;
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template