Articles on Trending Technologies

Technical articles with clear explanations and examples

PHP program to find the sum of cubes of the first n natural numbers

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 606 Views

To find the sum of cubes of the first n natural numbers, we can use a loop to iterate through each number and calculate its cube. The formula for the cube of a number is n³ = n × n × n. Example Here's a PHP program that calculates the sum of cubes for the first n natural numbers ? Output The sum of cubes of first 8 natural numbers is 1296 How It Works The sum_of_cubes() function takes a parameter $val representing the number of natural numbers ...

Read More

PHP program to find the sum of the first n natural numbers who are not powers of a specific number 'k'

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 478 Views

To find the sum of the first n natural numbers that are not powers of a specific number 'k', we calculate the total sum of natural numbers and subtract the powers of k that fall within the range. Algorithm The approach involves calculating the sum of all natural numbers from 1 to n using the formula n * (n + 1) / 2, then subtracting all powers of k that are less than or equal to n. Example Here's a PHP program that finds the sum of the first n natural numbers excluding powers of k ...

Read More

PHP program to find the sum of the 5th powers of first n natural numbers

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

To find the sum of the 5th powers of first n natural numbers, we need to calculate 15 + 25 + 35 + ... + n5. This can be implemented using a simple loop to iterate through numbers and accumulate their fifth powers. Example Here's a PHP function that calculates the sum of fifth powers of the first n natural numbers ? Output The sum of fifth powers of the first n natural numbers is 85648386825 How It Works The function sum_of_fifth_pow initializes a sum variable to 0. ...

Read More

PHP program to find the average of the first n natural numbers that are even

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 899 Views

To find the average of the first n natural numbers that are even, we need to calculate the sum of the first n even natural numbers and divide by n. The first n even natural numbers are 2, 4, 6, 8, ..., 2n. Mathematical Formula The average of the first n even natural numbers can be calculated using the formula: Average = (n + 1). This is because the sum of first n even numbers is n(n+1) and dividing by n gives us (n+1). Example Output The average of the ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 353 Views

To find the sum of cubes of natural numbers that are odd, we iterate through numbers, check if they are odd, and add their cubes to the sum. Example The following example calculates the sum of cubes of the first 8 odd natural numbers ? The sum of cubes of first 8 natural numbers that are odd is 6084 How It Works The function sum_of_cubes_odd() takes a parameter that specifies how many odd numbers to process. It uses a while loop to iterate through natural numbers starting from 1, ...

Read More

PHP program to check if all digits of a number divide it

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 322 Views

To check if all digits of a number divide it in PHP, we extract each digit and verify if it divides the original number without remainder. Zero digits are handled separately since division by zero is undefined. Example Here's a complete program to check if all digits of a number divide it − All digits divide the number 128 How It Works The divisibility_check() function verifies two conditions: the digit is not zero (to avoid division by zero) and the original number is completely divisible by the digit. The ...

Read More

PHP program to check if the total number of divisors of a number is even or odd

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

To check if the total number of divisors of a number is even or odd, we need to count all divisors and determine if the count itself is even or odd. Algorithm The efficient approach is to iterate from 1 to the square root of the number. For each divisor i found, we get two divisors: i and number/i. However, if i equals number/i (perfect square case), we count it only once. Example Output Number: 100 Total divisors: 9 The count of divisors is odd Number: 16 Total divisors: ...

Read More

PHP program to check if a year is leap year or not

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 14K+ Views

To check if a year is leap year or not in PHP, we need to apply the leap year rules. A year is a leap year if it is divisible by 400, or divisible by 4 but not by 100. Leap Year Rules The rules for determining a leap year are ? If a year is divisible by 400, it is a leap year If a year is divisible by 100 (but not 400), it is not a leap year If a year is divisible by 4 (but not 100), it is a leap year Otherwise, ...

Read More

PHP program to check if a number is prime or not

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 42K+ Views

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime in PHP, we can create a function that tests for divisibility by all numbers from 2 up to half of the given number. Example The following PHP function checks if a number is prime − Output It is a prime number How It Works The check_prime() function works by − First checking if the number is 1, which is ...

Read More

PHP program to split a given comma delimited string into an array of values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

To split a given comma delimited string into an array of values, PHP provides several built-in functions like explode() and preg_split(). Both methods effectively convert a comma-separated string into an array. Using explode() Function The explode() function is the most straightforward method to split a string by a specific delimiter ? The array is Array ( [0] => 456 [1] => 789 [2] => 23 [3] => 4 [4] => 019 ) ...

Read More
Showing 22351–22360 of 61,297 articles
Advertisements