Friday, December 27, 2024
Google search engine
HomeData Modelling & AIProgram to print numbers from N to 1 in reverse order

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




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


Java




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


Python3




# 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


C#




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


Javascript




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


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




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


Java




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


Python3




# 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


C#




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


Javascript




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


Output: 

5 4 3 2 1

 

Time Complexity: O(N)

Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments