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
Articles by AmitDiwan
Page 529 of 840
PHP program to find the sum of first n natural numbers that are divisible by a number 'x' or a number 'y'
To find the sum of first n natural numbers that are divisible by a number 'x' or a number 'y', we use the inclusion-exclusion principle. This mathematical approach counts numbers divisible by x, adds numbers divisible by y, then subtracts numbers divisible by both (LCM of x and y) to avoid double counting. Example The following code demonstrates how to calculate this sum − Output The sum of first 11 natural numbers divisible by 2 or 5 is 50 How It Works The function uses the arithmetic progression ...
Read MorePHP program to find the numbers within a given array that are missing
To find missing numbers from a range of positive integers, we can compare an existing array with an expected sequence. Here's a PHP program that finds the first few missing positive numbers from a given array. Example Output The missing numbers in the array are: 1 2 3 4 5 How It Works The missing_nums function first sorts the input array to process numbers in ascending order. It skips any negative numbers or zeros since we're looking for missing positive integers. The function then compares each expected number ...
Read MorePHP program to find the first natural number whose factorial can be divided by a number 'x'
To find the first natural number whose factorial can be divided by a number 'x', we need to calculate factorials incrementally and check divisibility until we find a match. Example The following PHP program finds the first natural number whose factorial is divisible by a given number ? Output The first natural number whose factorial can be divided by 16 is 4 How It Works The function factorial_num() starts with $fact_num = 1 and multiplies it by consecutive natural numbers (1, 2, 3, ...). At each step, it ...
Read MorePHP program to find if a number is present in a given sequence of numbers
In PHP, you can check if a specific number exists in an arithmetic sequence by comparing the target number with the sequence parameters. This involves checking if the number can be reached by adding a common difference repeatedly to a starting number. Example The following code demonstrates how to find if a number is present in a given arithmetic sequence − Is the number present in the sequence? Yes, it is present in the sequence How It Works The contains_in_sequence() function checks if a target number exists in an ...
Read MorePHP program to find the sum of cubes of the first n natural numbers
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 MorePHP program to find the sum of the first n natural numbers who are not powers of a specific number 'k'
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 MorePHP 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 More