Q:

Write a program in C# Sharp to print the first n natural number using recursion

0

 Write a program in C# Sharp to print the first n natural number using recursion. 
Test Data :
How many numbers to print : 10
Expected Output :
1 2 3 4 5 6 7 8 9 10

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
class RecExercise1
{
    static int printNatural(int stval, int ctr)
    {
	if (ctr < 1)
	{
	    return stval;
	}
	ctr--;
	Console.Write(" {0} ",stval);
	return printNatural(stval + 1, ctr);
    }
    static void Main()
    {
	Console.Write("\n\n Recursion : Print the first n natural number :\n");
	Console.Write("---------------------------------------------------\n");
	Console.Write(" How many numbers to print : ");
	int ctr= Convert.ToInt32(Console.ReadLine());
	// Call recursive method with two parameters.	
	printNatural(1, ctr);
	Console.Write("\n\n");
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now