-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
140 lines (128 loc) · 4.73 KB
/
Program.cs
File metadata and controls
140 lines (128 loc) · 4.73 KB
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
using System;
namespace Lab_01a
{
class Program
{
static void Main(string[] args)
{
try
{
StartSequence();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Console is complete");
}
}
// Calls all the other methods, prompts user for input and displays the results to the user
static void StartSequence()
{
Console.WriteLine("Welcome to my game! Let's do some math!");
Console.WriteLine("Please enter a number greater than zero");
string number = Console.ReadLine();
try
{
int convertedNumberforever = Convert.ToInt32(number);
if (number == "0")
{
Console.WriteLine("You entered 0, read the instructions again");
}
else
{
int[] numbersArray = new int[convertedNumber];
int[] finalUserNumbersArray = Populate(numbersArray);
int sum = GetSum(finalUserNumbersArray);
int product = GetProduct(finalUserNumbersArray, sum);
decimal quotient = GetQuotient(product);
Console.WriteLine($"Your Array size is: {finalUserNumbersArray.Length}");
string numbersOutput = "";
for (int i = 0; i < numbersArray.Length; i++)
{
if (i != numbersArray.Length - 1)
{
numbersOutput += $"{numbersArray[i]},";
}
else
{
numbersOutput += $"{numbersArray[i]}";
}
}
Console.WriteLine($"Your Array size is: {numbersOutput}");
Console.WriteLine($"The sum of the array is {sum}");
int numberChosen = product / sum;
Console.WriteLine($"{sum} * {numberChosen} = {product}");
decimal finalNumber = product / quotient;
Console.WriteLine($"{product} / {finalNumber} = {quotient}");
}
}
catch (FormatException e)
{
throw e.Message;
}
catch (OverflowException e)
{
throw e.Message;
}
}
// creates an array based on the users input
static int[] Populate(int[] userNumberArray)
{
for (int i = 1; i <= userNumberArray.Length; i++)
{
Console.WriteLine(i + "/" + userNumberArray.Length);
string newNumber = Console.ReadLine();
int convertedNewNumber = Convert.ToInt32(newNumber);
userNumberArray[i - 1] = convertedNewNumber;
}
return userNumberArray;
}
// calculates sum based on users input
static int GetSum(int[] numbers)
{
int sum = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
if (sum < 20)
{
throw new Exception($"Value of {sum} is too low");
}
return sum;
}
// calculates the product based on the users input
static int GetProduct(int[] array, int sum)
{
try
{
Console.WriteLine("Please pick a number between 1 and {0}", array.Length);
string userRandomIndex = Console.ReadLine();
int convertedUserRandomIndex = Convert.ToInt32(userRandomIndex) - 1;
int product = array[convertedUserRandomIndex] * sum;
return product;
}
catch (IndexOutOfRangeException e)
{
throw e;
}
}
// calculates quotient based on the users input
static decimal GetQuotient(int product)
{
Console.WriteLine("Enter a number to divide {0} by", product);
string userDivision = Console.ReadLine();
int convertedUserDivision = Convert.ToInt32(userDivision);
int productDivision = convertedUserDivision / product;
decimal quotient = decimal.Divide(product, convertedUserDivision);
if (convertedUserDivision == 0)
{
throw new DivideByZeroException($"You cannot divide {product} by 0, please try again");
}
return quotient;
}
}
}