¿Cómo imprimirá números del 1 al 100 sin usar el bucle? | Conjunto-2

Si observamos este problema detenidamente, podemos ver que la idea de «bucle» es rastrear algún valor de contador, por ejemplo, «i = 0» hasta «i <= 100». Entonces, si no se nos permite usar el bucle, ¿de qué otra manera se puede rastrear algo en lenguaje C?
Se puede hacer de muchas maneras para imprimir números utilizando cualquier condición de bucle como for(), while(), do-while(). Pero lo mismo se puede hacer sin usar bucles (usando funciones recursivas, instrucción goto).

La impresión de números del 1 al 100 usando funciones recursivas ya se ha discutido en el Set-1 . En esta publicación, se han discutido otros dos métodos: 

1. Usando la instrucción goto: 

C++

#include <iostream>
using namespace std;
 
int main()
{
    int i = 0;
     
begin:
    i = i + 1;
    cout << i << " ";
 
    if (i < 100)
    {
        goto begin;
    }
    return 0;
}
 
// This code is contributed by ShubhamCoder

C

#include <stdio.h>
 
int main()
{
    int i = 0;
begin:
    i = i + 1;
    printf("%d ", i);
 
    if (i < 100)
        goto begin;
    return 0;
}

C#

using System;
 
class GFG{
 
static public void Main ()
{
    int i = 0;
    begin:
        i = i + 1;
        Console.Write(" " + i + " ");
 
        if (i < 100)
        {
            goto begin;
        }
}
}
 
// This code is contributed by ShubhamCoder
Producción: 

1 2 3 4 . . . 97 98 99 100

 

2. Usando la función principal recursiva: 

C++

// C++ program to count all pairs from both the
// linked lists whose product is equal to
// a given value
#include <iostream>
using namespace std;
 
int main()
{
    static int i = 1;
     
    if (i <= 100)
    {
        cout << i++ << " ";
        main();
    }
    return 0;
}
 
// This code is contributed by ShubhamCoder

C

// C program to count all pairs from both the
// linked lists whose product is equal to
// a given value
#include <stdio.h>
 
int main()
{
    static int i = 1;
    if (i <= 100) {
        printf("%d ", i++);
        main();
    }
    return 0;
}

Java

// Java program to count all pairs from both the
// linked lists whose product is equal to
// a given value
class GFG
{
    static int i = 1;
 
    public static void main(String[] args)
    {
 
        if (i <= 100)
        {
            System.out.printf("%d ", i++);
            main(null);
        }
    }
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 program to count all pairs from both
# the linked lists whose product is equal to
# a given value
def main(i):
     
    if (i <= 100):
        print(i, end = " ")
        i = i + 1
        main(i)
         
i = 1
main(i)
 
# This code is contributed by SoumikMondal

C#

// C# program to count all pairs from both the
// linked lists whose product is equal to
// a given value
using System;
 
class GFG
{
    static int i = 1;
 
    public static void Main(String[] args)
    {
        if (i <= 100)
        {
            Console.Write("{0} ", i++);
            Main(null);
        }
    }
}
 
// This code is contributed by Rajput-Ji
Producción: 

1 2 3 4 . . . 97 98 99 100

 

Publicación traducida automáticamente

Artículo escrito por Pushpanjali chauhan 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 *