Logic operations, branching
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB

using System;
class Program {
static int Main(string[] args) {
// 1.
bool a, b, c;
a = true;
b = true;
c = false;
Console.WriteLine("a AND b = " + ((a && b) ? "TRUE" : "FALSE"));
Console.WriteLine("a OR b = " + ((a || b) ? "TRUE" : "FALSE"));
Console.WriteLine("a AND c = " + ((a && c) ? "TRUE" : "FALSE"));
Console.WriteLine("a OR (b AND c) = " + ((a || (b && c)) ? "TRUE" : "FALSE"));
Console.WriteLine("(a OR b) AND c = " + (((a || b) && c) ? "TRUE" : "FALSE"));
Console.WriteLine("c XOR b = " + ((c ^ b) ? "TRUE" : "FALSE"));
Console.WriteLine("a XOR b = " + ((a ^ b) ? "TRUE" : "FALSE"));
// 2.
a = true;
Console.Write("Please enter 0 or 1: ");
string res = Console.ReadLine();
if (res != "0" && res != "1") {
Console.WriteLine("That's not 0 or 1!");
return 1;
}
b = res == "1";
Console.WriteLine("a AND b = " + ((a && b) ? "TRUE" : "FALSE"));
Console.WriteLine("a OR b = " + ((a || b) ? "TRUE" : "FALSE"));
Console.WriteLine("a XOR b = " + ((a ^ b) ? "TRUE" : "FALSE"));
// 3.
Console.Write("Enter a number: ");
int num;
try {
num = Convert.ToInt32(Console.ReadLine());
} catch {
Console.WriteLine("That's not a number!");
return 1;
}
if (num % 2 == 0) Console.WriteLine("It's divisible by 2");
if (num % 3 == 0) Console.WriteLine("It's divisible by 3");
if (num % 4 == 0) Console.WriteLine("It's divisible by 4");
return 0;
}
}