C# For Dummies

Chapter 3. Operators and Expressions

1. Write an expression that checks whether an integer is odd or even.

Guidelines: Take the remainder of dividing the number by 2 and check if it is 0 or 1 (respectively the number is odd or even). Use % operator to calculate the remainder of integer division.

Solution:
static void Main(string[] args)
{
    int number = 23;
    bool even = number % 2 == 0 ? true : false;
    Console.WriteLine("{0} is even? {1}", number, even);
}

2. Write a Boolean expression that checks whether a given integer is divisible by both 5 and 7, without a remainder.

Guidelines: Use a logical \"AND\" (&& operator) and the remainder operation % in division. You can also solve the problem by only one test: the division of 35 (think why).

Solution:
static void Main(string[] args)
{
    int number = 36;
    bool divisible = number % 35 == 0 ? true : false;
    Console.WriteLine("{0} is divisible by both 5 and 7? {1}", number, even);
}

3. Write an expression that checks for a given integer if its third digit (right to left) is 7.

Guidelines: Divide the number by 100 and save it in a new variable, which then divide by 10 and take the remainder. The remainder of the division by 10 is the third digit of the original number. Check if it is equal to 7.

Solution:
static void Main(string[] args)
{
    int number = 45764;
    bool isSeven = (number / 100) % 10 == 7 ? true : false;
    Console.WriteLine("Third digit of {0} is 7", number, even);
}

4. Write an expression that checks whether the third bit in a given integer is 1 or 0.

Guidelines: Use bitwise \"AND\" on the current number and the number that has 1 only in the third bit (i.e. number 8, if bits start counting from 0).

Solution:
static void Main(string[] args)
{
    int number = 452;    
    Console.WriteLine("The third bit of {0} is 1? {1}", number, ((number >> 3) & 1) == 1);
}

5. Write an expression that calculates the area of a trapezoid by given sides a, b and height h.

Guidelines: The formula for trapezoid surface is: S = (a + b) * h / 2.

Solution:
static void Main(string[] args)
{
    float a = 2;
    float b = 3;
    float h = 1;
    Console.WriteLine("S={0}", (a + b) / (2 * h));
}

6. Write a program that prints on the console the perimeter and the area of a rectangle by given side and height entered by the user.

Guidelines: Search the Internet for how to read integers from the console and use the formula for rectangle area calculation. If you have difficulties see instructions on the next problem.

Solution:
static void Main(string[] args)
{
    Console.Write("Enter a: ");
    int a = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter b: ");
    int b = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("S={0}, P={1}", a * b, (a + b) * 2);
}

7. The gravitational field of the Moon is approximately 17% of that on the Earth. Write a program that calculates the weight of a man on the moon by a given weight on the Earth.

Guidelines: Read the number from the console, multiply it by 0.17 and print it.

Solution:
static void Main(string[] args)
{
    Console.WriteLine("Enter weight of a man: ");
    int weight = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("This person will weight {0}kg on the Moon.", weight * 0.17);
}

Write an expression that checks for a given point {x, y} if it is within the circle K[{0, 0}, R=5]. Explanation: the point {0, 0} is the center of the circle and 5 is the radius.

Guidelines: Use the Pythagorean Theorem a2 + b2 = c2. The point is inside the circle when (x*x) + (y*y) ≤ 5*5.

Solution:
static void Main(string[] args)
{
    Console.Write("Enter x: ");
    int x = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter y: ");
    int y = Convert.ToInt32(Console.ReadLine());
    bool isInside = (x * * + y * y <= 5) ? true : false;
    Console.WriteLine("The point O({0},{1}) is inside K((0,0),5)?: {2}", x, y, isInside);
}

9. Write an expression that checks for given point {x, y} if it is within the circle K[{0, 0}, R=5] and out of the rectangle [{-1, 1}, {5, 5}]. Clarification: for the rectangle the lower left and the upper right corners are given.

Guidelines: Use the code from the previous task and add a check for the rectangle. A point is inside a rectangle with walls parallel to the axes, when in the same time it is right of the left wall, left of the right wall, down from the top wall and above the bottom wall.

Solution: Link
10. Write a program that takes as input a four-digit number in format abcd (e.g. 2011) and performs the following actions:
-Calculates the sum of the digits (in our example 2+0+1+1 = 4).
-Prints on the console the number in reversed order: dcba (in our example 1102).
-Puts the last digit in the first position: dabc (in our example 1201).
-Exchanges the second and the third digits: acbd (in our example 2101).

Guidelines: To get the individual digits of the number you can divide by 10 and take the remainder of the division by 10 four times.

Solution: Link
11. We are given number n and position p. Write a sequence of operations that prints the value of the bit on the position p in the number (0 or 1). Example: n=35, p=5 -> 1. Another example: n=35, p=6 -> 0.

Guidelines: Use bitwise operations

Solution: Link
12. Write a Boolean expression that checks if the bit on position p in the integer v has the value 1. Example v=5, p=1 -> false.

Guidelines: The task is similar to the previous one.

Solution:
static void Main(string[] args)
{
    int v = 350;
    int p = 350;
    int mask = 1 << p;
    bool isOne = (v & mask) != 0 ? true : false;
    Console.WriteLine("The bit at position {0}of number {1} is 1? {2}", p, v, isOne);
}

13. We are given the number n, the value v (v = 0 or 1) and the position p. write a sequence of operations that changes the value of n, so the bit on the position p has the value of v. Example: n=35, p=5, v=0 -> n=3. Another example: n=35, p=2, v=1 -> n=39.

Guidelines: Use bitwise operations by analogy with the previous two problems.

Solution:
static void Main(string[] args)
{
    int n = 350;
    int v = 0;
    int p = 3;
    n = (v == 0) ? n = n & (~(1 << p)) : n = n | (1 << p);
    Console.WriteLine(n);
}

14. Write a program that checks if a given number n (1 < n < 100) is a prime number (i.e. it is divisible without remainder only to itself and 1).

Guidelines: Read about loops in the Internet. Use a loop and check the number for divisibility by all integers from 1 to the square root of the number. Since n < 100, you can find in advance all prime numbers from 1 to 100 and checks the input over them. The prime numbers in the range [1…100] are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 and 97.

Solution:
static void Main(string[] args)
{
    int number = 72;
    bool isPrime = true;
    if (number > 2)
        for (int i = 2; i <= Math.Ceiling(Math.Sqrt(numbber)); ++i)
        {
            if (number % i == 0) isPrime = false;
        }
    Console.WriteLine("{0} is prime?: {1}", number, isPrime);
}

15. Write a program that exchanges the values of the bits on positions 3, 4 and 5 with bits on positions 24, 25 and 26 of a given 32-bit unsigned integer.

Guidelines: Use 3 times a combination of getting and setting a bit at a given position.

Solution:
Link
16. Write a program that exchanges bits {p, p+1, …, p+k-1} with bits {q, q+1, …, q+k-1} of a given 32-bit unsigned integer.

Guidelines: Extend the solution of the previous problem to perform a sequence of bit exchanges in a loop.

Solution:
Link


← Back