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++
// 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 |
5 4 3 2 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.
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 |
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.
Recommended Posts:
- Print prime numbers from 1 to N in reverse order
- Print words of a string in reverse order
- Print Fibonacci Series in reverse order
- Print Doubly Linked list in Reverse Order
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Print Strings In Reverse Dictionary Order Using Trie
- Program to copy the contents of one array into another in the reverse order
- Program to print reverse character bridge pattern
- Program to print Reverse Floyd's triangle
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Print all numbers in given range having digits in strictly increasing order
- Reverse Level Order Traversal
- Merge two sorted linked lists such that merged list is in reverse order
- Sort an array where a subarray of a sorted array is in reverse order
- Sort first k values in ascending order and remaining n-k values in descending order
- Print a given matrix in reverse spiral form
- Print Reverse a linked list using Stack
- Print a matrix in Reverse Wave Form
- Print reverse string after removing vowels
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
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.

