Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP Articles
Page 39 of 81
PHP program to find the sum of the 5th powers of first n natural numbers
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 MorePHP program to find the average of the first n natural numbers that are even
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 MorePHP 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, 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 MorePHP program to check if all digits of a number divide it
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 MorePHP program to check if the total number of divisors of a number is even or odd
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 MorePHP program to check if a year is leap year or not
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 MorePHP program to check if a number is prime or not
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 MorePHP program to split a given comma delimited string into an array of values
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 MorePHP program to remove non-alphanumeric characters from string
To remove non-alphanumeric characters from a string in PHP, you can use the preg_replace() function with regular expressions. This function searches for a pattern and replaces it with specified content. Method 1: Using [^a-z0-9] Pattern The most common approach uses a negated character class to match anything that's not alphanumeric − The non-alphanumeric characters removed gives the string as Thisissampleonly The pattern /[^a-z0-9]/i means: [^a-z0-9] − Match any character that is NOT a lowercase letter or digit i flag − Makes the pattern case-insensitive Method 2: ...
Read MorePHP program to convert a given timestamp into time ago
In PHP, converting a timestamp to a human-readable "time ago" format is useful for displaying relative time in applications like social media feeds or comment systems. This can be achieved using a custom function that calculates the time difference and formats it appropriately. Example The following example demonstrates how to convert a timestamp into a "time ago" format ? The timestamp to time ago conversion is 10 minutes ago 1 hour ago 1 day ago How It Works The to_time_ago() function calculates the difference between the current time and ...
Read More