- Related Questions & Answers
- PHP program to find the sum of cubes of the first n natural numbers
- PHP program to find the average of the first n natural numbers that are even
- PHP program to find the sum of the 5th powers of first n natural numbers
- Java program to find the sum of n natural numbers
- PHP program to find the sum of odd numbers within a given range
- PHP program to calculate the sum of square of first n natural numbers
- 8085 program to find the sum of first n natural numbers
- C++ program to Find Sum of Natural Numbers using Recursion
- How to Find the Sum of Natural Numbers using Python?
- Program to find sum of first n natural numbers in C++
- C++ Program to Calculate Sum of Natural Numbers
- Program to find the sum of first n odd numbers in Python
- Summing cubes of natural numbers within a range in JavaScript
- PHP program to find the sum of the first n natural numbers who are not powers of a specific number ‘k’
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’

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

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

<?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); ?>

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