Found 1046 Articles for PHP

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

AmitDiwan
Updated on 02-Jul-2020 07:06:10

382 Views

To find the sum of cubes of the first n natural numbers, the code is as follows −Example Live DemoOutputThe sum of cubes of first 8 natural numbers is 1296A function named ‘sum_of_cubes’ is defined that initializes a sum to 0. Every natural number is multiplied by itself thrice (cube) and added to the initial sum. The limit is the value that is passed as a parameter to the function. The limit is defined outside the function and the function is called. Relevant output is displayed on the console.

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

AmitDiwan
Updated on 02-Jul-2020 07:05:04

307 Views

To find the sum of the first n natural numbers who are not powers of a specific number ‘k’, the code is as follows −Example Live DemoOutputThe sum of fist 20 natural numbers that are not powers of 3 is 198A function named ‘sum_of_nums’ is defined and it calculates the sum of natural numbers that are not powers of a certain value. The number and the non-power number are passed as parameters to this function. Outside the function, a value each for n and k is defined and the function is called on these values. Relevant output is displayed on the ... Read More

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

AmitDiwan
Updated on 02-Jul-2020 07:03:38

132 Views

To find the sum of the 5th powers of first n natural numbers, the code is as follows −Example Live DemoOutputThe sum of fifth powers of the first n natural numbers is 85648386825A function named ‘sum_of_fifth_pow’ is defined, and an initial sum value is defined as 0. The fifth power of every natural number up to a certain range is computed and added to the initial sum value. Outside the function, a value is defined and the function is called by passing this value as parameter. Relevant message is displayed on the console.

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

AmitDiwan
Updated on 02-Jul-2020 07:02:20

587 Views

To find the average of the first n natural numbers that are even, the code is as follows −Example Live DemoOutputThe average of the first n natural numbers that are even is 12A function named ‘even_nums_avg’ is defined that adds up all the numbers up to a given limit, and divides it by total numbers of values. This is the average of the first few natural numbers. A value is defined, and the function is called by passing this value, which is actually the limit up to which the average of numbers needs to be found. Relevant message is displayed on ... Read More

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

AmitDiwan
Updated on 02-Jul-2020 07:01:25

207 Views

To find the sum of cubes of natural numbers that are odd, the code is as follows −Example Live DemoOutputThe sum of cubes of first 8 natural numbers that are even is 3968A 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 ... Read More

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

AmitDiwan
Updated on 02-Jul-2020 07:00:08

169 Views

To check if all digits of a number divide it in PHP, the code is as follows −Example Live Demo

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

AmitDiwan
Updated on 02-Jul-2020 06:58:31

168 Views

To check if the total number of divisors of a number is even or odd, the code is as follows −Example Live DemoOutputIt is an odd numberA function named ‘divisor_count’ is defined that gives the number of divisors of a given number that is passed as a parameter to the function. Now, each of these divisors is checked to see if it can be completely divided by 2, if yes, it is an even divisor, and otherwise, it is an odd divisor. Relevant message is displayed on the console.

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

AmitDiwan
Updated on 20-Dec-2021 12:29:09

11K+ Views

To check if a year is leap year or not in PHP, the code is as follows −Example Live DemoOutputIt is not a leap yearA function named ‘year_check’ is defined that takes a year as a parameter. It checks to see if the year can be divided by 400 or 4 completely, if yes, it means it is a leap year. Otherwise, it is a leap year. The value for year is defined outside the function, and the function is called by passing this year as a parameter to it. Relevant output is displayed on the console.

PHP program to check if a number is prime or not

AmitDiwan
Updated on 13-Sep-2023 14:35:19

33K+ Views

To check if a number is prime or not, the code is as follows −Example Live DemoOutputIt is a prime numberA function named check_prime() is used to check if the number is prime or not. A number that needs to be checked for being prime is passed as a parameter to the function. The number is defined and this function is called by passing the number as parameter. Relevant message is displayed on the console.

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

AmitDiwan
Updated on 02-Jul-2020 06:54:26

2K+ Views

To split a given comma delimited string into an array of values, the PHP code is as follows −Example Live DemoOutputThe array is Array (    [0] => 456    [1] => 789    [2] => 23    [3] => 4    [4] => 019 ) The array is Array (    [0] => 00    [1] => 876    [2] => 5432    [3] => 1234    [4] => 0 )A string of numbers is defined and the ‘preg_split’ function is used to separate the numbers based on the occurance of ‘/’ or ‘.’. The split array is now printed. Another method to split the given number in the form of a string is to use the explode function. It splits the array based on the occurance of a comma.

Advertisements