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

C++Server Side ProgrammingProgramming

Positive integers 1, 2, 3, 4... are known as natural numbers.

This program takes a positive integer from the user ( suppose user-entered n ) then, this program displays the value of 13+23+33+....+n3.

Input: n = 3
Output: 36

Explanation

13+23+33 = 1 +8+27 = 36

This program takes a positive integer from user( suppose user entered n ) then, this program displays the value of 13+23+33+....+n3.

Example

#include <iostream>
using namespace std;
int main() {
   int n = 3;
   int sum = 0;
   for (int x=1; x<=n; x++)
      sum += x*x*x;
   cout << sum;
   return 0;
}
raja
Published on 20-Aug-2019 08:55:36
Advertisements