Print numbers from 1 to n using recursion in typescript

Ts program for Print numbers from 1 to n using recursion. Here mentioned other language solution.

// TypeScript program for
// Print numbers from 1 to n using recursion
class Numbers
{
	public printNumber(num: number)
	{
		if (num >= 1)
		{
			// Reduce the number and try again 
			// Until n is greater than zero
			this.printNumber(num - 1);
			// Display calculated result
			console.log(" " + num);
		}
	}
	public static main()
	{
		var task = new Numbers();
		// Test A (1..10)
		var num = 10;
		console.log("\n (1 to " + num + ") : ");
		task.printNumber(num);
		// Test B (1..20)
		num = 20;
		console.log("\n (1 to " + num + ") : ");
		task.printNumber(num);
	}
}
Numbers.main();
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */

Output

 (1 to 10) :
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

 (1 to 20) :
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved