-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinedCalc.cs
More file actions
179 lines (165 loc) · 6.1 KB
/
CombinedCalc.cs
File metadata and controls
179 lines (165 loc) · 6.1 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
namespace CombinedCalc
{
internal class Program
{
// Pseudo Code:
// Step 1: Prompt Menu
// Step 2: Select option from Menu
// Step 3: Check if option is 0 (if it is 0 break if not continue program)
// Step 4: Perform action based on what is entered by users
// Step 5: Repeat Steps 1 to 4
// Main Method codes will start from here usually
static void Main(string[] args)
{
// Declaring of Variables
bool exit = false; // Assigning value to this variable so it can be reassigned to end the loop
// Loop to constantly continue program till 0 is selected
// Menu Looping Option 1 (Using Switch)
while (exit == false)
{
// Prompt display of Menu and user input
Menu();
Console.Write("Enter choice: ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
{
CalcBMI();
Console.WriteLine();
exit = false;
break;
}
case "2":
{
CalcDiscount();
Console.WriteLine();
exit = false;
break;
}
case "3":
{
MultiplicationTable();
Console.WriteLine();
exit = false;
break;
}
case "0":
{
// end of program;
Console.WriteLine("Bye");
exit = true;
break;
}
default:
{
Console.WriteLine("Invalid Option! Please try again.");
Console.WriteLine();
break;
}
}
}
/*
// Menu Looping Option 2 (Using if-else)
while (exit == false)
{
// Prompt display of Menu and user input
Menu();
Console.Write("Enter choice: ");
string choice = Console.ReadLine();
if (choice == "1") {
CalcBMI();
Console.WriteLine();
exit = false;
}
else if (choice == "2") {
CalcDiscount();
Console.WriteLine();
exit = false;
}
else if (choice == "3") {
MultiplicationTable();
Console.WriteLine();
exit = false;
}
else if (choice == "0") {
// end of program;
Console.WriteLine("Bye");
exit = true;
}
else {
Console.WriteLine("Invalid Option! Please try again.");
Console.WriteLine();
}
}
*/
}
// Code for Main Menu
public static void Menu()
{
Console.WriteLine("----------------------- MENU -----------------------");
Console.WriteLine("[1] Calculate Body Mass Index");
Console.WriteLine("[2] Calculate Discount");
Console.WriteLine("[3] Display Multiplication Table");
Console.WriteLine("[0] Exit");
Console.WriteLine("----------------------------------------------------");
}
// Code for BMI Calculator
public static void CalcBMI()
{
// try catch statements are for exception handling
try
{
Console.Write("Enter your heuight in meters: ");
double height = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter your weight in kg: ");
double weight = Convert.ToDouble(Console.ReadLine());
double BMI = weight / (height * height);
Console.WriteLine("The BMI of a person with the height of " + height + " and weight of " + weight + "kg is " + BMI);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// Code to Calculate Discount
public static void CalcDiscount()
{
// try catch statements are for exception handling
try
{
Console.Write("Enter the price of product: ");
double cost = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the discount percentage (%): ");
double discountpercentage = Convert.ToDouble(Console.ReadLine());
double totalprice = cost * ((100 - discountpercentage) / 100);
Console.WriteLine("The cost of the product after " + discountpercentage + "% is " + totalprice);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// Code for Displaying of Multiplication Table
public static void MultiplicationTable()
{
// try catch statements are for exception handling
try
{
Console.Write("Enter number to multiply: ");
int numbertomultiply = Convert.ToInt32(Console.ReadLine());
// looping to print out the multiplication table up to 15)
for (int i = 0; i < 16; i++)
{
int product = i * numbertomultiply;
Console.WriteLine(i + " X " + numbertomultiply + " = " + product);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}