-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
126 lines (100 loc) · 3.24 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rekursia
{
internal class Program
{
static int[] GenerateRandomArray()
{
Console.Write("Size of the array: ");
if (!int.TryParse(Console.ReadLine(), out int length) || length <= 0)
{
Dekor();
Console.Write("Invalid Array");
Dekor();
return new int[0];
}
int[] array = new int[length];
Random random = new Random();
for (int i = 0; i < length; i++)
{
array[i] = random.Next(100);
}
return array;
}
static void GenerateManualArray(int[] array, int index = 0)
{
if (index < array.Length)
{
Console.Write((index + 1) + " Number = ");
int.TryParse(Console.ReadLine(), out array[index]);
index++;
GenerateManualArray(array, index);
}
}
static void OutputArray(int[] array, int startFromIndex = 0)
{
if (startFromIndex != array.Length)
{
Console.Write(array[startFromIndex] + "\t");
startFromIndex++;
OutputArray(array, startFromIndex);
}
}
static int SumArray(int[] array, int startFromIndex = 0, int elseSum = 0)
{
if (startFromIndex != array.Length)
{
elseSum = elseSum + array[startFromIndex];
startFromIndex++;
return SumArray(array, startFromIndex, elseSum);
}
else return elseSum;
}
static int NumberSum(int number, int elseSum = 0)
{
if (number != 0)
{
elseSum += number % 10;
number = number / 10;
return NumberSum(number, elseSum);
}
return elseSum;
}
static void Dekor()
{
Console.WriteLine();
for (int i = 0; i < 120; i++)
{
Console.Write("-");
}
}
static void Main(string[] args)
{
int[] myArray = GenerateRandomArray();
if (myArray.Length == 0)
return;
//GenerateManualArray(myArray); //If you want to enter an array manually
Dekor();
Console.Write("Your array:\t");
OutputArray(myArray);
Dekor();
Console.Write("\nThe sum of array numbers = " + SumArray(myArray));
Dekor();
Console.Write("The number to calculate its sum: ");
if(!int.TryParse(Console.ReadLine(), out int number))
{
Dekor();
Console.Write("Invalid Number");
Dekor();
return;
}
Dekor();
Console.Write("Sum of digits = " + NumberSum(number));
Dekor();
}
}
}