PHP

Write a PHP program to print all natural numbers from 1 to N1 min read

In PHP, while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop than for loop.

PHP program to print all natural numbers from 1 to N (ALGORITHM)

Step 1: Accept the number from the user and insert it into the variable num




Step 2: Assign the value of variable num into the variable n and assign the value 0 into the variable sum

Step 3: Perform the following sub-steps until the condition ‘n >= 0’

        (i) Assign the calculated value of ‘sum + n’ into the variable sum

        (ii) Decrement the value of the variable n

Step 4: Print the value of the variable sum as the sum of n natural number

PHP Source Code:

Output:

Write a PHP program to print all natural numbers from 1 to N
Write a PHP program to print all natural numbers from 1 to N

Leave a Comment