Adding Conditional Statements to Program.cs
This commit is contained in:
110
Program.cs
110
Program.cs
@@ -1,4 +1,5 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace opg1
|
namespace opg1
|
||||||
{
|
{
|
||||||
@@ -16,8 +17,28 @@ namespace opg1
|
|||||||
Console.WriteLine($"ModuloOperations(8, 5, 2): {ModuloOperations(8, 5, 2)}");
|
Console.WriteLine($"ModuloOperations(8, 5, 2): {ModuloOperations(8, 5, 2)}");
|
||||||
Console.WriteLine($"CubeOf(2): {CubeOf(2)}");
|
Console.WriteLine($"CubeOf(2): {CubeOf(2)}");
|
||||||
Console.WriteLine($"CubeOf(-5.5): {CubeOf(-5.5)}");
|
Console.WriteLine($"CubeOf(-5.5): {CubeOf(-5.5)}");
|
||||||
Console.WriteLine($"SwapTwoNumbers(87, 45): {SwapTwoNumbers(87, 45)}");
|
Console.WriteLine($"SwapTwoNumbers(87, 45): {string.Join(", ", SwapTwoNumbers(87, 45))}");
|
||||||
Console.WriteLine($"SwapTwoNumbers(-13, 2): {SwapTwoNumbers(-13, 2)}");
|
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)
|
static int AddAndMultiply(int a, int b, int c)
|
||||||
@@ -51,7 +72,7 @@ namespace opg1
|
|||||||
|
|
||||||
static int ModuloOperations(int a, int b, int c)
|
static int ModuloOperations(int a, int b, int c)
|
||||||
{
|
{
|
||||||
return (a % b) / c;
|
return (a % b) % c;
|
||||||
}
|
}
|
||||||
|
|
||||||
static double CubeOf(double a)
|
static double CubeOf(double a)
|
||||||
@@ -59,17 +80,88 @@ namespace opg1
|
|||||||
return a * a * a;
|
return a * a * a;
|
||||||
}
|
}
|
||||||
|
|
||||||
static string SwapTwoNumbers(int a, int b)
|
static List<int> SwapTwoNumbers(int a, int b)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
return [b, a];
|
||||||
|
}
|
||||||
|
|
||||||
sb.Append($"Before: a = {a}, b = {b}; ");
|
static int AbsoluteValue(int a)
|
||||||
|
{
|
||||||
|
return Math.Abs(a);
|
||||||
|
}
|
||||||
|
|
||||||
(a, b) = (b, a);
|
static int DivisibleBy2Or3(int a, int b)
|
||||||
|
{
|
||||||
|
if (_DivisibleBy2Or3(a) && _DivisibleBy2Or3(b))
|
||||||
|
{
|
||||||
|
return a * b;
|
||||||
|
}
|
||||||
|
|
||||||
sb.Append($"After: a = {a}, b = {b}");
|
|
||||||
|
|
||||||
return sb.ToString();
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user