Server Side Programming Articles - Page 1734 of 2650

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

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

222 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

849 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

306 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

256 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

243 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

14K+ 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

41K+ 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.

PHP program to remove non-alphanumeric characters from string

AmitDiwan
Updated on 02-Jul-2020 06:53:14

1K+ Views

To remove non-alphanumeric characters from string, the code is as follows −Example Live DemoOutputThe non-alphanumeric characters removed gives the string as ThisissampleonlyThe function ‘preg_replace’ is used to remove alphanumeric characters from the string. A regular expression is used to filter out the alphanumeric characters. The string is previously defined and the function ‘preg_replace’ is called on this and the reformed string is displayed on the console.Example Live DemoOutputThe non-alphanumeric characters removed gives the string as Thisisasample_onlyThe only difference here is that a different regular expression is used. It means the same as the previous regular expression, but written in a different fashion.Read More

PHP program to compute the execution time of a PHP script

AmitDiwan
Updated on 02-Jul-2020 06:51:46

697 Views

To compute the execution time of a PHP script, the code is as follows −Example Live DemoOutputThe execution time of the PHP script is : 1.69 secThe ‘microtime’ function can be used to check the time taken by a PHP script to execute completely. When the code begins execution, the time is recorded, and once the code is completed, another timestamp is generated and the difference between the end and star time is the time taken by the script to complete its execution.

Advertisements