Programa para imprimir números del N al 1 en orden inverso

Dado un número N , la tarea es imprimir los números de N a 1 .
Ejemplos: 

Entrada: N = 10 
Salida: 10 9 8 7 6 5 4 3 2 1
Entrada: N = 7 
Salida: 7 6 5 4 3 2 1   

Enfoque 1: ejecute un ciclo de N a 1 e imprima el valor de N para cada iteración. Disminuya el valor de N en 1 después de cada iteración.
A continuación se muestra la implementación del enfoque anterior. 

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>
Producción: 

5 4 3 2 1

 

Complejidad de tiempo: O(N)

Espacio Auxiliar: O(1)

Enfoque 2: Usaremos la recursividad para resolver este problema.
 

  1. Compruebe el caso base. Aquí es N<=0.
  2. Si se cumple la condición base, vuelve a la función principal.
  3. Si la condición base no se cumple, imprima N y llame a la función recursivamente con el valor (N – 1) hasta que se cumpla la condición base.

A continuación se muestra la implementación del enfoque anterior. 
 

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>
Producción: 

5 4 3 2 1

 

Complejidad de tiempo: O(N)

Espacio Auxiliar: O(N)

Publicación traducida automáticamente

Artículo escrito por hrishikeshkonderu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *