📌  相关文章
📜  程序以相反的顺序从N到1打印数字

📅  最后修改于: 2021-05-14 01:53:04             🧑  作者: Mango

给定数字N ,任务是将数字从N打印到1
例子:

方法1:运行从N到1的循环,并为每次迭代打印N的值。每次迭代后,将N的值减1。
下面是上述方法的实现。

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


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


输出:
5 4 3 2 1

时间复杂度: O(N)

辅助空间: O(1)

方法2:我们将使用递归来解决此问题。

  1. 检查基本情况。在这里,N <= 0。
  2. 如果满足基本条件,则返回到main函数。
  3. 如果不满足基本条件,则打印N并以值(N – 1)递归调用该函数,直到满足基本条件为止。

下面是上述方法的实现。

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

Java脚本


输出:
5 4 3 2 1

时间复杂度: O(N)

辅助空间: O(1)