167 lines
5.7 KiB
C#
167 lines
5.7 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace BasicProgramming
|
|
{
|
|
internal class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
Console.WriteLine("====== Basic Programming ======");
|
|
Console.WriteLine($"AddAndMultiply: {AddAndMultiply(2, 4, 5)}");
|
|
Console.WriteLine($"CToF(0): {CtoF(0)}");
|
|
Console.WriteLine($"CToF(100): {CtoF(100)}");
|
|
Console.WriteLine($"CToF(-300): {CtoF(-300)}");
|
|
Console.WriteLine($"ElementaryOperations(3, 8): {string.Join(", ", ElementaryOperations(3, 8))}");
|
|
Console.WriteLine($"IsResultTheSame(2+2, 2*2): {IsResultTheSame(2 + 2, 2 * 2)}");
|
|
Console.WriteLine($"IsResultTheSame(9/3, 16-1): {IsResultTheSame(9 / 3, 16 - 1)}");
|
|
Console.WriteLine($"ModuloOperations(8, 5, 2): {ModuloOperations(8, 5, 2)}");
|
|
Console.WriteLine($"CubeOf(2): {CubeOf(2)}");
|
|
Console.WriteLine($"CubeOf(-5.5): {CubeOf(-5.5)}");
|
|
Console.WriteLine($"SwapTwoNumbers(87, 45): {string.Join(", ", SwapTwoNumbers(87, 45))}");
|
|
Console.WriteLine($"SwapTwoNumbers(-13, 2): {string.Join(", ", SwapTwoNumbers(-13, 2))}");
|
|
Console.WriteLine("====== Conditional Statements ======");
|
|
Console.WriteLine($"AbsoluteValue(6832): {AbsoluteValue(6832)}");
|
|
Console.WriteLine($"AbsoluteValue(-392): {AbsoluteValue(-392)}");
|
|
Console.WriteLine($"DivisibleBy2Or3(15, 30): {DivisibleBy2Or3(15, 30)}");
|
|
Console.WriteLine($"DivisibleBy2Or3(2, 90): {DivisibleBy2Or3(2, 90)}");
|
|
Console.WriteLine($"DivisibleBy2Or3(7, 12): {DivisibleBy2Or3(7, 12)}");
|
|
Console.WriteLine($"IfConsistsOfUppercaseLetters('xyz'): {IfConsistsOfUppercaseLetters("xyz")}");
|
|
Console.WriteLine($"IfConsistsOfUppercaseLetters('DOG'): {IfConsistsOfUppercaseLetters("DOG")}");
|
|
Console.WriteLine($"IfConsistsOfUppercaseLetters('L9#'): {IfConsistsOfUppercaseLetters("L9#")}");
|
|
Console.WriteLine($"IfGreaterThanThirdOne([2, 7, 12]) : {IfGreaterThanThirdOne([2, 7, 12])}");
|
|
Console.WriteLine($"IfGreaterThanThirdOne([-5, -8, 50]): {IfGreaterThanThirdOne([-5, -8, 50])}");
|
|
Console.WriteLine($"IfNumberIsEven(721): {IfNumberIsEven(721)}");
|
|
Console.WriteLine($"IfNumberIsEven(1248): {IfNumberIsEven(1248)}");
|
|
Console.WriteLine($"IfSortedAscending([3, 7, 10]): {IfSortedAscending([3, 7, 10])}");
|
|
Console.WriteLine($"IfSortedAscending([74, 62, 99]): {IfSortedAscending([74, 62, 99])}");
|
|
Console.WriteLine($"PositiveNegativeOrZero(5.24): {PositiveNegativeOrZero(5.24)}");
|
|
Console.WriteLine($"PositiveNegativeOrZero(0.0): {PositiveNegativeOrZero(0.0)}");
|
|
Console.WriteLine($"PositiveNegativeOrZero(-994.53): {PositiveNegativeOrZero(-994.53)}");
|
|
Console.WriteLine($"IfYearIsLeap(2016): {IfYearIsLeap(2016)}");
|
|
Console.WriteLine($"IfYearIsLeap(2018): {IfYearIsLeap(2018)}");
|
|
}
|
|
|
|
static int AddAndMultiply(int a, int b, int c)
|
|
{
|
|
return (a + b) * c;
|
|
}
|
|
|
|
static string CtoF(int celcius)
|
|
{
|
|
if (celcius <= -271.15)
|
|
return "Temperature below absolute zero!";
|
|
|
|
double fahrenheit = (celcius * 1.8) + 32;
|
|
|
|
return $"T = {fahrenheit}F";
|
|
}
|
|
|
|
static List<double> ElementaryOperations(double a, double b)
|
|
{
|
|
List<double> retval = [a + b, a - b, a * b];
|
|
if (a > 0 && b > 0)
|
|
retval.Add(a / b);
|
|
|
|
return retval;
|
|
}
|
|
|
|
static bool IsResultTheSame(double a, double b)
|
|
{
|
|
return a == b;
|
|
}
|
|
|
|
static int ModuloOperations(int a, int b, int c)
|
|
{
|
|
return (a % b) % c;
|
|
}
|
|
|
|
static double CubeOf(double a)
|
|
{
|
|
return a * a * a;
|
|
}
|
|
|
|
static List<int> SwapTwoNumbers(int a, int b)
|
|
{
|
|
return [b, a];
|
|
}
|
|
|
|
static int AbsoluteValue(int a)
|
|
{
|
|
return Math.Abs(a);
|
|
}
|
|
|
|
static int DivisibleBy2Or3(int a, int b)
|
|
{
|
|
if (_DivisibleBy2Or3(a) && _DivisibleBy2Or3(b))
|
|
{
|
|
return a * b;
|
|
}
|
|
|
|
|
|
static bool _DivisibleBy2Or3(int x)
|
|
{
|
|
return (x % 3) == 0 || (x % 2) == 0;
|
|
}
|
|
|
|
return a + b;
|
|
}
|
|
|
|
static bool IfConsistsOfUppercaseLetters(string str)
|
|
{
|
|
return str.Equals(Regex.Match(str, "[A-ZÆØÅ]+").Value);
|
|
}
|
|
|
|
static bool IfGreaterThanThirdOne(List<int> ints)
|
|
{
|
|
if (ints.Count < 3)
|
|
{
|
|
throw new Exception("Must have 3 or more ints");
|
|
}
|
|
|
|
return (ints[0] * ints[1]) > ints[2];
|
|
}
|
|
|
|
static bool IfNumberIsEven(int a)
|
|
{
|
|
return a % 2 == 0;
|
|
}
|
|
|
|
static bool IfSortedAscending(List<int> ints)
|
|
{
|
|
int lastNumber = 0;
|
|
bool retval = true;
|
|
|
|
ints.ForEach(x =>
|
|
{
|
|
if (lastNumber < x)
|
|
{
|
|
lastNumber = x;
|
|
}
|
|
else
|
|
{
|
|
retval = false;
|
|
}
|
|
});
|
|
|
|
return retval;
|
|
}
|
|
|
|
static string PositiveNegativeOrZero(double a)
|
|
{
|
|
switch (a)
|
|
{
|
|
case > 0:
|
|
return "positive";
|
|
case < 0:
|
|
return "negative";
|
|
default:
|
|
return "zero";
|
|
}
|
|
}
|
|
|
|
static bool IfYearIsLeap(int year)
|
|
{
|
|
return DateTime.IsLeapYear(year);
|
|
}
|
|
}
|
|
} |