-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
85 lines (72 loc) · 3.19 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
using static System.Net.Mime.MediaTypeNames;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Assignment_1_2
{
internal class Program
{
private static double PerformArithmatic(double a, double b, string mathType)
{
switch (mathType)
{
case "add":
return a + b;
case "subtract":
return a - b;
case "multiply":
return a * b;
case "divide":
if (b != 0)
return a / b;
else
throw new DivideByZeroException("The divisor cannot be zero.");
default: throw new ArgumentException("Invalid math type.");
}
}
static void Main(string[] args)
{
/* Assignment 1.2.1
* Write a C# Sharp program to accept two integers and check whether they are equal or not.
* Test Data :
* Input 1st number: 5
* Input 2nd number: 5
* Expected Output :
* 5 and 5 are equal
* Test Data :
* Input 1st number: 5
* Input 2nd number: 15
* Expected Output :
* 5 and 15 are not equal */
Console.WriteLine("Input an integer:");
int integerOne = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nInput a second integer:");
int integerTwo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine((integerOne == integerTwo) ? $"\n{integerOne} and {integerTwo} are equal." : $"\n{integerOne} and {integerTwo} are not equal.");
/* Assignment 1.2.2
* Write a C# Sharp program to find the sum of first 10 natural numbers.
* Expected Output :
* The first 10 natural number is :
* 1 2 3 4 5 6 7 8 9 10
* The Sum is : 55 */
int sum = 0;
for (int i = 0; i < 10; i++)
sum += i + 1;
Console.WriteLine("\n\nThe first 10 natural numbers are:\n1 2 3 4 5 6 7 8 9 10");
Console.WriteLine($"\nThe sum of these numbers is: {sum}");
/* Assignment 1.2.3
* Write a menu driven application to perform calculation functions like
* addition, subtraction, multiplication, and division.
* Call them appropriately when user selects the option.
* Give the user the option to continue or exit the program. */
string? mathType;
Console.WriteLine("\n\nThe different types of arithmatic calculations: \nadd subtract multiply divide");
do
{
Console.WriteLine("\nWhat kind of math problem would you like to perform?");
mathType = Console.ReadLine();
}
while (mathType != "add" && mathType != "subtract" && mathType != "multiply" && mathType != "divide");
double sumOfMath = PerformArithmatic(integerOne, integerTwo, mathType);
Console.WriteLine($"\n{integerOne} {mathType} {integerTwo} equals {sumOfMath}.");
}
}
}