
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP program to find the sum of cubes of the first n natural numbers
To find the sum of cubes of the first n natural numbers, the code is as follows −
Example
<?php function sum_of_cubes($val) { $init_sum = 0; for ($x = 1; $x <= $val; $x++) $init_sum += $x * $x * $x; return $init_sum; } print_r("The sum of cubes of first 8 natural numbers is "); echo sum_of_cubes(8); ?>
Output
The sum of cubes of first 8 natural numbers is 1296
A function named ‘sum_of_cubes’ is defined that initializes a sum to 0. Every natural number is multiplied by itself thrice (cube) and added to the initial sum. The limit is the value that is passed as a parameter to the function. The limit is defined outside the function and the function is called. Relevant output is displayed on the console.
- Related Articles
- PHP program to find the sum of the 5th powers of first n natural numbers
- PHP program to find the sum of cubes of natural numbers that are odd
- PHP program to calculate the sum of square of first n natural numbers
- 8085 program to find the sum of first n natural numbers
- Find the sum of first $n$ odd natural numbers.
- Program to find sum of first n natural numbers in C++
- PHP program to find the average of the first n natural numbers that are even
- PHP program to find the sum of the first n natural numbers who are not powers of a specific number ‘k’
- Java Program to cube sum of first n natural numbers
- Java program to find the sum of n natural numbers
- C Program for the cube sum of first n natural numbers?
- Sum of first n natural numbers in C Program
- Java Program to Display Numbers and Sum of First N Natural Numbers
- Swift Program to Calculate Cube Sum of First n Natural Numbers
- Java Program to calculate Sum of squares of first n natural numbers

Advertisements