c# midterm1 exercises from deitel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace midterm1console
{
class Program
{
static void Main(string[] args)
{
int choose;
string menu = "1-GEOMETRIC CALCULATION\n" +
"2-MULTIPLE TEST\n" +
"3-RANDOM NUMBER TEST\n" +
"4-PALINDROME TEST\n" +
"5-PHONE NUMBER ENCRYPT\n" +
"6-FUNCTION OVERLOADING\n";
Console.WriteLine(menu);
Console.WriteLine("choose one of them: ");
choose = int.Parse(Console.ReadLine());
switch (choose)
{
case 1:
double pi = Math.PI;
double radius, area, circum, diameter;
int num1, num2;
////GEOMETRIC CALCULATER TEST
Console.WriteLine("enter the radius: ");
radius = double.Parse(Console.ReadLine());
area = pi * radius * radius;
Console.WriteLine("area: {0}", area);
diameter = 2 * radius;
Console.WriteLine("diameter: {0}", diameter);
circum = 2 * pi * radius;
Console.WriteLine("circumference: {0}", circum);
break;
case 2:
///MULTIPLE TEST
Console.WriteLine("\n\n#############################\n\n");
Console.WriteLine("enter num1: ");
num1 = int.Parse(Console.ReadLine());
Console.WriteLine("enter num2: ");
num2 = int.Parse(Console.ReadLine());
if ((num1 % num2) == 0)
Console.WriteLine("the first number is a multiple of the second.");
else
Console.WriteLine("the first number is not a multiple of the second.");
break;
case 3:
//RANDOM NUMBER TEST
Random random = new Random(System.DateTime.Now.Millisecond);
int num = random.Next(1, 1000);
Console.WriteLine("\n\nrandom: {0}", num);
break;
case 4:
///PALINDROME TEST
Console.WriteLine("\n\nenter five-digit-number: ");
int palindrome;
palindrome = int.Parse(Console.ReadLine());
string test = palindrome.ToString();
try
{
if (test[0] == test[4] && test[1] == test[3])
Console.WriteLine("\n\nit is a palindrome");
else
Console.WriteLine("\n\nit is not a palindrome");
}
catch (Exception e)
{
if (test.Length != 5)
Console.WriteLine("You must enter five digit!");
else
Console.WriteLine("something was wrong!");
}
break;
case 5:
//ENCRYPT PHONE NUMBER
int phone_num;
string temp, crypt = "";
Console.WriteLine("\n\nenter phone num: ");
phone_num = int.Parse(Console.ReadLine());
temp = phone_num.ToString();
for (int i = 0; i < temp.Length; i++)
{
string c = temp[i].ToString();
int x = int.Parse(c);
crypt = crypt + ((x + 7) % 10).ToString();
}
Console.WriteLine("crypted num: {0}", crypt);
//DECRYPT PHONE NUMMER
int phone;
string decrypt = "";
for (int i = 0; i < crypt.Length; i++)
{
string c = crypt[i].ToString();
int x = int.Parse(c);
if (10 == (10 + x) - 7)
decrypt = decrypt + (0).ToString();
else if (11 == (10 + x) - 7)
decrypt = decrypt + (1).ToString();
else if (12 == (10 + x) - 7)
decrypt = decrypt + (2).ToString();
else
decrypt = decrypt + ((10 + x) - 7).ToString();
}
phone = int.Parse(decrypt);
Console.WriteLine("decrypted num: {0}", phone);
break;
//FUNCTION OVERLOADING TEST
case 6:
int k = 2, m = 5;
double n = 3.5, h = 5.25;
Console.WriteLine("integer addition result: {0}", add(k, m));
Console.WriteLine("double addition reslult: {0}", add(n, h));
break;
default:
Console.WriteLine("enter only 1-5 !!!");
break;
}
Console.ReadKey();
}
static int add(int x, int y)
{
return x + y;
}
static double add(double x, double y)
{
return x + y;
}
}
}