-
Notifications
You must be signed in to change notification settings - Fork 0
/
19-RecursionAlgorithms.cs
142 lines (113 loc) · 3.75 KB
/
19-RecursionAlgorithms.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Text;
namespace HackerRank
{
public class RecursionAlgorithms_Result
{
public static int Factorial(int n)
{
#region By-Loop-Iteration
//int result=1;
//for(int i=n; i > 0; i--)
//{
// result = result * i;
//}
//return result;
#endregion
#region By-Recursion
//base criteria
if (n == 0)
{ return 1; }
//progressive criteria
return n * Factorial(n - 1);
#endregion
}
public static void PrintNaturalNumber_AscendingOrder(int number, int start)
{
#region Ascending Order
if (number > 0)
{
Console.WriteLine(start);
start++;
PrintNaturalNumber_AscendingOrder(number-1, start);
}
#endregion
}
public static void PrintNaturalNumber_DescendingOrder(int number)
{
#region Iteration
//for(int i = 1; i <= number; i++)
//{
// Console.WriteLine(i);
//}
#endregion
#region ReverseOrder
if (number > 0)
{
Console.WriteLine(number);
PrintNaturalNumber_DescendingOrder(number - 1);
}
#endregion
}
public static int SumOfNaturalNumbers(int num)
{
if (num == 0)
{ return 1; }
return num + SumOfNaturalNumbers(num - 1);
}
public static void SeparateDigits(int n)
{
if (n < 10)
{
Console.Write("{0} ", n);
return;
}
SeparateDigits(n / 10);
Console.Write(" {0} ", n % 10);
}
public static int MultiplyByExponent(int exp, int number)
{
if (exp == 0)
{
return 1;
}
return number * MultiplyByExponent(exp - 1, number);
}
}
public class RecursionAlgorithms_Solution
{
public static void Input()
{
#region Program-1 Factorial
//int number = 5;
//int result = RecursionAlgorithms_Result.Factorial(number);
//Console.WriteLine($"Factorial of {number} is: {result}");
#endregion
#region Program-2(a) NaturalNumbers_AscendingOrder
int num = 10;
int start = 1;
RecursionAlgorithms_Result.PrintNaturalNumber_AscendingOrder(num,start);
#endregion
#region Program-2(b) NaturalNumbers_DescendingOrder
//int num = 10;
//RecursionAlgorithms_Result.PrintNaturalNumber_DescendingOrder(num);
#endregion
#region Program-3 SumOfNaturalNumbers
//int num = 10;
//int result = RecursionAlgorithms_Result.SumOfNaturalNumbers(num);
//Console.WriteLine($"Sum of first {num} natural numbers: {result}");
#endregion
#region Program-4 Convert Number to individual digits
//int number = 123;
//RecursionAlgorithms_Result.SeparateDigits(number);
#endregion
#region Program-5 Multiply number by its Exponent
//int exponent = 3;
//int number = 4;
//int Result= RecursionAlgorithms_Result.MultiplyByExponent(exponent, number);
//Console.WriteLine($"Result of {number} exp {3} is:{Result}");
#endregion
}
}
}