PHP Articles

Page 38 of 81

Swap two variables in one line in C/C++, Python, PHP and Java

Hafeezul Kareem
Hafeezul Kareem
Updated on 15-Mar-2026 669 Views

In this tutorial, we are going to learn how to swap two variables in different languages. Swapping means interchanging the values of two variables. Let's see an example. Input a = 3 b = 5 Output a = 5 b = 3 Let's see how to achieve this in different programming languages. Python We can swap variables with one line of code in Python using tuple unpacking − Example # initializing the variables a, b = 3, 5 # printing before swapping print("Before swapping:-", a, b) ...

Read More

PHP program to calculate the sum of square of first n natural numbers

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 699 Views

To calculate the sum of square of first n natural numbers in PHP, we can use a simple loop to iterate through each number and add its square to the running total. Example Here's a PHP function that calculates the sum of squares ? Output The sum of square of first 5 natural numbers is 55 How It Works A function named sum_of_squares is defined that takes the limit up to which square of natural numbers needs to be found. In this function, a sum value is initialized ...

Read More

PHP program to calculate the repeated subtraction of two numbers

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 314 Views

Repeated subtraction is a method to find how many times one number can be subtracted from another until the result becomes less than the subtractor. This is mathematically equivalent to division and finding the quotient. Example Here's a PHP program that calculates repeated subtraction using a recursive approach ? The repeated subtraction results in 18 How It Works The function repeated_sub uses the Euclidean algorithm principle. It checks if the first value divides evenly by the second value. If yes, it returns the floor of the quotient. Otherwise, it ...

Read More

PHP program to find the sum of odd numbers within a given range

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

In PHP, you can find the sum of odd numbers within a given range using different approaches. Here's a mathematical approach that uses a formula to calculate the sum efficiently. Using Mathematical Formula This method calculates the sum of odd numbers using the mathematical property that the sum of first n odd numbers equals n²? The sum of odd natural numbers between 3 and 23 is 169 Using Loop Method A simpler approach using a loop to iterate through the range and add odd numbers? ...

Read More

PHP program to find the sum of first n natural numbers that are divisible by a number 'x' or a number 'y'

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 226 Views

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 More

PHP program to find the numbers within a given array that are missing

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 638 Views

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 More

PHP program to find the first natural number whose factorial can be divided by a number 'x'

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 252 Views

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 More

PHP program to find if a number is present in a given sequence of numbers

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 Views

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 More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 598 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 469 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
Showing 371–380 of 802 articles
« Prev 1 36 37 38 39 40 81 Next »
Advertisements