Program to print numbers from N to 1 in reverse order - GeeksforGeeks

26 Mar, 2021
Given a number N, the task is to print the numbers from N to 1.Examples: 
Input: N = 10 Output: 10 9 8 7 6 5 4 3 2 1Input: N = 7 Output: 7 6 5 4 3 2 1   
Approach 1: Run a loop from N to 1 and print the value of N for each iteration. Decrement the value of N by 1 after each iteration.Below is the implementation of the above approach. 
C++
Java
Python3
C#
Javascript
// C++ program to print all numbers between 1// to N in reverse order #include <bits/stdc++.h>using namespace std; // Recursive function to print from N to 1void PrintReverseOrder(int N){     for (int i = N; i > 0; i--)        cout << i << " "; } // Driven Codeint main(){    int N = 5;     PrintReverseOrder(N);     return 0;}
// Java program to print all numbers between 1// to N in reverse orderimport java.util.*; class GFG { // Recursive function to print from N to 1static void PrintReverseOrder(int N){     for (int i = N; i > 0; i--)        System.out.print( +i + " ");} // Driver codepublic static void main(String[] args){    int N = 5;     PrintReverseOrder(N);}} // This code is contributed by shivanisinghss2110
# Python3 program to print all numbers# between 1 to N in reverse order # Recursive function to print# from N to 1def PrintReverseOrder(N):         for i in range(N, 0, -1):        print(i, end = " "); # Driver codeif __name__ == '__main__':         N = 5;    PrintReverseOrder(N); # This code is contributed by 29AjayKumar
// C# program to print all numbers// between 1 to N in reverse orderusing System; class GFG{ // Recursive function to print// from N to 1static void PrintReverseOrder(int N){    for(int i = N; i > 0; i--)       Console.Write(i + " ");} // Driver codepublic static void Main(String[] args){    int N = 5;     PrintReverseOrder(N);}} // This code is contributed by Rajput-Ji
<script>     // Javascript program to print all numbers between 1    // to N in reverse order         // Recursive function to print from N to 1    function PrintReverseOrder(N)    {         for (let i = N; i > 0; i--)            document.write(i + " ");     }      let N = 5;      PrintReverseOrder(N);     </script>
5 4 3 2 1
 
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach 2: We will use recursion to solve this problem. 
Check for the base case. Here it is N<=0.If base condition satisfied, return to the main function.If base condition not satisfied, print N and call the function recursively with value (N – 1) until base condition satisfies.
Check for the base case. Here it is N<=0.
If base condition satisfied, return to the main function.
If base condition not satisfied, print N and call the function recursively with value (N – 1) until base condition satisfies.
Below is the implementation of the above approach.  
C++
Java
Python3
C#
Javascript
// C++ program to print all numbers between 1// to N in reverse order #include <bits/stdc++.h>using namespace std; // Recursive function to print from N to 1void PrintReverseOrder(int N){    // if N is less than 1    // then return void function    if (N <= 0) {        return;    }    else {        cout << N << " ";         // recursive call of the function        PrintReverseOrder(N - 1);    }} // Driven Codeint main(){    int N = 5;     PrintReverseOrder(N);     return 0;}
// Java program to print all numbers// between 1 to N in reverse orderclass GFG{ // Recursive function to print// from N to 1static void PrintReverseOrder(int N){         // If N is less than 1 then    // return static void function    if (N <= 0)    {        return;    }    else    {        System.out.print(N + " ");         // Recursive call of the function        PrintReverseOrder(N - 1);    }} // Driver codepublic static void main(String[] args){    int N = 5;     PrintReverseOrder(N);}} // This code is contributed by 29AjayKumar
# Python3 program to print all numbers between 1# to N in reverse order # Recursive function to print from N to 1def PrintReverseOrder(N):     # if N is less than 1    # then return void function    if (N <= 0):        return;    else:        print(N, end = " ");         # recursive call of the function        PrintReverseOrder(N - 1);         # Driver CodeN = 5;PrintReverseOrder(N); # This code is contributed by Nidhi_biet
// C# program to print all numbers// between 1 to N in reverse orderusing System;class GFG{ // Recursive function to print// from N to 1static void PrintReverseOrder(int N){         // If N is less than 1 then    // return static void function    if (N <= 0)    {        return;    }    else    {        Console.Write(N + " ");         // Recursive call of the function        PrintReverseOrder(N - 1);    }} // Driver codepublic static void Main(){    int N = 5;     PrintReverseOrder(N);}} // This code is contributed by Code_Mech
<script>    // Javascript program to print all numbers between 1    // to N in reverse order         // Recursive function to print from N to 1    function PrintReverseOrder(N)    {             // if N is less than 1        // then return void function        if (N <= 0)        {            return;        }        else        {            document.write(N + " ");             // recursive call of the function            PrintReverseOrder(N - 1);        }    }         // Driver code    let N = 5;    PrintReverseOrder(N);         // This code is contributed by suresh07. </script>
5 4 3 2 1
 
Time Complexity: O(N)
Auxiliary Space: O(1)
shivanisinghss2110
Rajput-Ji
29AjayKumar
Code_Mech
nidhi_biet
subhammahato348
divyeshrabadiya07
suresh07
Natural Numbers
Reverse
Arrays
Mathematical
Recursion
School Programming
Arrays
Mathematical
Recursion
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers