Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Print N numbers without using any loops.
using System;
namespace PrintValues
{
class PrintTest
{
static void Main(string[] args)
{
Console.WriteLine("Please enter from and to numbers\n");
string[] numbers = Console.ReadLine().Split();
int fromNumber = int.Parse(numbers[0]);
int toNumber = int.Parse(numbers[1]);
PrintTest objPrintTest = new PrintTest();
Console.WriteLine("\nList of numbers with Recursion");
Console.WriteLine("----------------");
objPrintTest.PrintWithRecursion(fromNumber, toNumber);
Console.WriteLine("\n\nList of numbers with goto");
Console.WriteLine("----------------");
objPrintTest.PrintWithGoto(fromNumber, toNumber);
Console.Read();
}
//printing the numbers using recursion
void PrintWithRecursion(int fromNumber, int toNumber)
{
if (fromNumber <= toNumber)
{
Console.Write(fromNumber + " ");
fromNumber++;
PrintWithRecursion(fromNumber, toNumber);
}
}
//Printing the numbers using goto statement.
void PrintWithGoto(int fromNumber, int toNumber)
{
Repeat:
if (fromNumber <= toNumber)
{
Console.Write(fromNumber + " ");
fromNumber++;
goto Repeat;
}
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment