The Wayback Machine - https://web.archive.org/web/20240619141135/https://www.geeksforgeeks.org/php-program-to-print-numbers-from-n-to-1-in-reverse-order/
Open In App

PHP Program to Print Numbers from N to 1 in Reverse Order

Last Updated : 20 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to print numbers from N to 1 in reverse order using PHP. This is a common programming task that can be accomplished using different approaches, such as loops or recursion.

Examples:

Input: N = 10
Output: 10 9 8 7 6 5 4 3 2 1

Input: N = 15
Output: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Below are the approaches to print numbers from N to 1 in reverse order in PHP:

Using For Loop

One of the simplest method to print numbers in reverse order is by using a for loop. In this approach we define the printReverseNums() function which takes an integer $n as an argument and uses a for loop to print numbers from $n to 1. The loop starts with $i = $n and decrements $i by 1 in each iteration until $i is greater than or equal to 1.

Example: This example shows the use of the above-explained approach.

PHP
<?php

function printReverseNums($n) {
    for ($i = $n; $i >= 1; $i--) {
        echo $i . " ";
    }
}

printReverseNums(10);

?>

Output
10 9 8 7 6 5 4 3 2 1 

Using While Loop

Another method to print numbers in reverse order is by using a while loop. In this approach, we define the printReverseNums() function which uses a while loop to print numbers from $n to 1. The loop continues as long as $i is greater than or equal to 1, and $i is decremented by 1 in each iteration.

Example: This example shows the use of the above-explained approach.

PHP
<?php

function printReverseNums($n) {
    $i = $n;
    while ($i >= 1) {
        echo $i . " ";
        $i--;
    }
}

printReverseNums(10);

?>

Output
10 9 8 7 6 5 4 3 2 1 

Using Recursion

Recursion is used to solve those problems where some logic is being repeated multiple times. In this approach, we define the printReverseNums() function which uses recursion to print numbers from $n to 1. The function calls itself with $n – 1 as the argument until $n is less than 1, which is the base condition that stops the recursion.

Example: This example shows the use of the above-explained approach.

PHP
<?php

function printReverseNums($n) {
    if ($n >= 1) {
        echo $n . " ";
        printReverseNums($n - 1);
    }
}

printReverseNums(10);

?>

Output
10 9 8 7 6 5 4 3 2 1 

Similar Reads

Php Program to Print a given matrix in reverse spiral form
Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Output:
3 min read
How to get elements in reverse order of an array in PHP ?
An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements of an array : Table of Content Using for LoopUsing
3 min read
JavaScript Program to Print all Even Numbers in a Sorted Order of a Linked List
This JavaScript program is designed to identify all even numbers in a sorted order of a linked list. A linked list is a linear data structure where elements, known as nodes, are connected via pointers. The program iterates through the linked list to determine the all-even numbers in the sorted order it contains. There are several ways to print all
3 min read
Javascript Program For Merging Two Sorted Linked Lists Such That Merged List Is In Reverse Order
Given two linked lists sorted in increasing order. Merge them such a way that the result list is in decreasing order (reverse order). Examples: Input: a: 5-&gt;10-&gt;15-&gt;40 b: 2-&gt;3-&gt;20 Output: res: 40-&gt;20-&gt;15-&gt;10-&gt;5-&gt;3-&gt;2 Input: a: NULL b: 2-&gt;3-&gt;20 Output: res: 20-&gt;3-&gt;2Recommended: Please solve it on "PRACTIC
4 min read
Javascript Program to Print a given matrix in reverse spiral form
Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Output:
3 min read
JavaScript Program to Print Reverse Floyd Pattern Triangle Pyramid
Printing a reverse Floyd pattern triangle pyramid involves outputting a series of numbers in descending order, forming a triangular shape with each row containing one less number than the previous row. The numbers are arranged in such a way that they form a right-angled triangle, reminiscent of Floyd's triangle, but in reverse order. Example: Input
2 min read
PHP Program to Print All Even Numbers in a Range
Given two numbers, Start and End, the task is to print all even numbers within a given range using PHP. Even numbers are integers that are divisible by 2, meaning they have no remainder when divided by 2. Examples: Input: start = 1, end = 10Output: 2 4 6 8 10Input: start = 21, end = 35Output: 22 24 26 28 30 32 34These are the following approaches:
3 min read
Write a program to display Reverse of any number in PHP ?
Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 It can be achieved using Iterative way or Recursive Way Iterative Method: Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num &gt; 0 (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_n
2 min read
PHP Program to Reverse Bit of a Number
Given a number N, the task is to Reverse the Bit of the number and convert back into a decimal number in PHP. Examples: Input: N = 11Output: 13Explanations: (11)10 = (1011)2 After revering the Bits (1101)2 = (13)10Approach 1: Reverse Bit of a Number using Bitwise Shift OperatorsIn this approach, we obtain each bit of the binary representation of th
2 min read
How to use map() on an array in reverse order with JavaScript ?
Given a JavaScript array and the task is to apply the map() method but on the reverse of the array efficiently. Here are a few approaches discussed. If you don't want to change the original array then you can create a shallow copy of the array after that you can perform the task. Approaches to use array.map() in reverse order:Using JavaScript array
3 min read