Program to print numbers from N to 1 in reverse order

• Difficulty Level : Easy
• Last Updated : 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 1
Input: 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++

 // C++ program to print all numbers between 1// to N in reverse order #include 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

 // 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

 # 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#

 // 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

Javascript


Output:
5 4 3 2 1

Time Complexity: O(N)

Auxiliary Space: O(1)

Approach 2: We will use recursion to solve this problem.

1. Check for the base case. Here it is N<=0.
3. 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++

 // C++ program to print all numbers between 1// to N in reverse order #include 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

 // 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

 # 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#

 // 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

Javascript


Output:
5 4 3 2 1

Time Complexity: O(N)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up