- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# PHP program to find the sum of cubes of natural numbers that are odd

To find the sum of cubes of natural numbers that are odd, the code is as follows −

## Example

<?php function sum_of_cubes_even($val) { $init_sum = 0; for ($i = 0; $i < $val; $i++) if ($i % 2 != 0) $init_sum += (2 * $i) * (2 * $i) * (2 * $i); return $init_sum; } print_r("The sum of cubes of first 8 natural numbers that are even is "); echo sum_of_cubes_even(8); ?>

## Output

The sum of cubes of first 8 natural numbers that are even is 3968

A function named ‘sum_of_cubes_even’ is defined that takes a limit on the natural number up to which the cube of every number needs to be found and each on them need to be added up. A ‘for’ loop is iterated over, and every number is cubed and added to an initial sum that was initially 0. The function is called by passing a number that is the limit as parameter. Relevant message is displayed on the console.

Advertisements