Program to print numbers from N to 1 in reverse order

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 1
void PrintReverseOrder(int N)
{
  
    for (int i = N; i > 0; i--)
        cout << i << " ";
  
}
  
// Driven Code
int main()
{
    int N = 5;
  
    PrintReverseOrder(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all numbers between 1
// to N in reverse order
import java.util.*;
  
class GFG {
  
// Recursive function to print from N to 1
static void PrintReverseOrder(int N)
{
  
    for (int i = N; i > 0; i--)
        System.out.print( +i + " ");
}
  
// Driver code
public static void main(String[] args)
{
    int N = 5;
  
    PrintReverseOrder(N);
}
}
  
// This code is contributed by shivanisinghss2110

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print all numbers 
# between 1 to N in reverse order
  
# Recursive function to print 
# from N to 1
def PrintReverseOrder(N):
      
    for i in range(N, 0, -1):
        print(i, end = " ");
  
# Driver code
if __name__ == '__main__':
      
    N = 5;
    PrintReverseOrder(N);
  
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all numbers 
// between 1 to N in reverse order
using System;
  
class GFG{
  
// Recursive function to print
// from N to 1
static void PrintReverseOrder(int N)
{
    for(int i = N; i > 0; i--)
       Console.Write(i + " ");
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 5;
  
    PrintReverseOrder(N);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

5 4 3 2 1

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

  1. Check for the base case. Here it is N<=0.
  2. If base condition satisfied, return to the main function.
  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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 1
void 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 Code
int main()
{
    int N = 5;
  
    PrintReverseOrder(N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print all numbers 
// between 1 to N in reverse order
class GFG{
  
// Recursive function to print 
// from N to 1
static 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 code
public static void main(String[] args)
{
    int N = 5;
  
    PrintReverseOrder(N);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print all numbers between 1
# to N in reverse order
  
# Recursive function to print from N to 1
def 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 Code
N = 5;
PrintReverseOrder(N);
  
# This code is contributed by Nidhi_biet

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print all numbers 
// between 1 to N in reverse order
using System;
class GFG{
  
// Recursive function to print 
// from N to 1
static 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 code
public static void Main()
{
    int N = 5;
  
    PrintReverseOrder(N);
}
}
  
// This code is contributed by Code_Mech

chevron_right


Output:

5 4 3 2 1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.