

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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.
- 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
- PHP program to find the sum of odd numbers within a given range
- Java program to find the sum of n natural numbers
- PHP program to calculate the sum of square of first n natural numbers
- 8085 program to find the sum of first n natural numbers
- Java Program to Find the Sum of Natural Numbers using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- Java Program to Calculate the Sum of Natural Numbers
- C++ Program to Calculate Sum of Natural Numbers
- Program to find sum of first n natural numbers in C++
- How to Find the Sum of Natural Numbers using Python?
- Summing cubes of natural numbers within a range in JavaScript
- Program to find the sum of first n odd numbers in Python
Advertisements