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

📅  最后修改于: 2021-09-05 08:53:03             🧑  作者: Mango

给定一个数字N ,任务是打印从N 到 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)

方法二:我们将使用递归来解决这个问题。

  1. 检查基本情况。这里是 N<=0。
  2. 如果满足基本条件,则返回主函数。
  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

蟒蛇3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live