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.

66 lines
1.9 KiB

using System;
class Program {
static void Main(string[] args) {
//1. Declaring bool variables and doing boolean logick
bool a = true;
bool b = true;
bool c = false;
Console.WriteLine("a AND b = " + ( a && b));
Console.WriteLine("a OR b = " + ( a || b));
Console.WriteLine("a AND c = " + ( a && c));
Console.WriteLine("a OR (b AND c) = " + ( a || (b && c)));
Console.WriteLine("(a OR b) AND c = " + ((a || b) && c));
Console.WriteLine("c XOR b = " + ( c ^ b));
Console.WriteLine("a XOR b = " + ( a ^ b));
//2. Asking for bool value and doing boolean logick with it
bool correct = false;
ConsoleKeyInfo cki;
Console.WriteLine("\nEnter 0 or 1.");
//Asking repeatedly until the user gives correct input
while(!correct){
cki = Console.ReadKey(true);
if(cki.KeyChar == '0'){
Console.WriteLine("0 -> False");
b = false;
correct = true;
}
if(cki.KeyChar == '1'){
Console.WriteLine("1 -> True");
b = true;
correct = true;
}
}
Console.WriteLine("a AND b = " + ( a && b));
Console.WriteLine("a OR b = " + ( a || b));
Console.WriteLine("a XOR b = " + ( a ^ b));
//3. Asking for a number (I used double) and checking divisibility
double n;
bool d2 = false;
bool d3 = false;
bool d4 = false;
Console.WriteLine("\nEnter a number.");
//Asking repeatedly until the user gives correct input (while the input cannot be converted to double)
while(!double.TryParse(Console.ReadLine(), out n)){}
if(n%3 == 0){
d3 = true;
}
if(n%4 == 0){
d2 = true;
d4 = true;
}else{
if(n%2 == 0){
d2 = true;
}
}
Console.WriteLine("{0} | 2 = {1}", n, d2);
Console.WriteLine("{0} | 3 = {1}", n, d3);
Console.WriteLine("{0} | 4 = {1}", n, d4);
//Waiting for input before closing
Console.ReadLine();
}
}