# C# For Dummies

## Chapter 2. Primitive Types and Variables

Declare several variables by selecting for each one of them the most appropriate of the types `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long` and `ulong` in order to assign them the following values: 52,130; -115; 4825932; 97; -10000; 20000; 224; 970,700,000; 112; -44; -1,000,000; 1990; 123456789123456789.

Guidelines: Look at the ranges of the numerical types in C#.

Solution:
class Program
{
static void Main()
{
ushort number1 = 52130;
sbyte number2 = -115;
uint number3 = 4825932;
byte number4 = 97;
short number5 = -10000;
short number6 = 20000;
byte number7 = 224;
uint number8 = 970700000;
sbyte number9 = 112;
sbyte number10 = -44;
int number11 = -1000000;
ushort number12 = 1992;
long number13 = 123456789123456789;
}
}

2. Which of the following values can be assigned to variables of type `float`, `double` and `decimal`: 5, -5.01, 34.567839023; 12.345; 8923.1234857; 3456.091124875956542151256683467?

Guidelines: Consider the number of digits after the decimal point. Refer to the table with sizes of the types `float`, `double` and `decimal`.

Solution:
class Program
{
static void Main()
{
double number1 = 34.567839023;
float number2 = 12.345f;
double number3 = 8923.1234857;
decimal number4 = 3456.091124875956542151256683467m;
}
}

3. Write a program, which compares correctly two real numbers with accuracy at least 0.000001.

Guidelines: Two floating-point variables are considered equal if their difference is less than some predefined precision (e.g. 0.000001).

Solution:
class Program
{
static void Main()
{
decimal number1 = 5.25745243896m;
decimal number2 = 9.8544531763m;
number1 += number2;
Console.WriteLine(number1.ToString("#.######"));
}
}

4. Initialize a variable of type int with a value of 256 in hexadecimal format (256 is 100 in a numeral system with base 16).

Guidelines: Look at the section about Integer Literals. To easily convert numbers to a different numeral system use the built-in Windows calculator. For a hexadecimal representation of the literal use prefix 0x.

Solution:
class Program
{
static void Main(string[] args)
{
int number = 0x100;
}
}

5. Declare a variable of type `char` and assign as a value the character, which has Unicode code, 72 (use the Windows calculator in order to find hexadecimal representation of 72).

Solution:
class Program
{
static void Main(string[] args)
{
int number = 0x100;
}
}

6. Declare a variable `isMale` of type `bool` and assign a value to it depending on your gender.

Solution:
class Program
{
static void Main(string[] args)
{
bool isMale = true;
}
}

7. Declare two variables of type `string` with values "Hello" and "World". Declare a variable of type `object`. Assign to this variable the value obtained of concatenation of the two string variables (add space if necessary). Print the variable of type `object`.

Solution:
class Program
{
static void Main(string[] args)
{
string str1 = "Hello";
string str2 = "World";
object obj = str1 + " " + str2;
}
}

8. Declare two variables of type `string` and assign them values "Hello" and "World". Declare a variable of type `object` and assign to it the value obtained of concatenation of the two variables of type string (do not miss the space in the middle). Declare a third variable of type `string` and initialize it with the value of the variable of type `object` (you should use type casting).

Solution:
class Program
{
static void Main(string[] args)
{
string str1 = "Hello";
string str2 = "World";
object obj = str1 + " " + str2;
string str3 = obj.ToString();
}
}

9. Declare two variables of type `string` and assign them a value “The "use" of quotations causes difficulties.” (without the outer quotes). In one of the variables use quoted string and in the other do not use it.

Guidelines: Read about Character Literals. It is necessary to use the escaping character "\" or verbatim strings.

Solution:
class Program
{
static void Main(string[] args)
{
string str1 = "The \"use\" of quotations causes difficulties.";
string str2 = "The " + "\u0022" + "use" + "\u0022" + " of quotations causes difficulties";
}
}

10. Write a program to print a figure in the shape of a heart by the sign \"o\".

Guidelines: Use Console.WriteLine(…), the character 'o' and spaces.

Solution:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("  0        0 ");
Console.WriteLine("0   0    0   0");
Console.WriteLine(" 0   0  0   0");
Console.WriteLine("  0   00   0");
Console.WriteLine("   0      0 ");
Console.WriteLine("    0    0  ");
Console.WriteLine("     0  0   ");
Console.WriteLine("      00   ");
}
}

11. Write a program that prints on the console isosceles triangle which sides consist of the copyright character \"©\".

Guidelines: Use Console.WriteLine(…), the character © and spaces. Use Windows Character Map in order to find the Unicode code of the sign \"©\".

Solution:
class Program
{
static void Main(string[] args)
{
}
}

12. A company dealing with marketing wants to keep a data record of its employees. Each record should have the following characteristic – first name, last name, age, gender (‘m’ or ‘f’) and unique employee number (27560000 to 27569999). Declare appropriate variables needed to maintain the information for an employee by using the appropriate data types and attribute names.

Guidelines: For the names use type `string`, for the gender use type `char` (only one char `m/f`), and for the unique number and age use some integer type.

Solution:
class Program
{
static void Main(string[] args)
{
string firstName;
string lastName;
byte age;
char gender;
int id;
}
}

13. Declare two variables of type `int`. Assign to them values 5 and 10 respectively. Exchange (swap) their values and print them.

Guidelines: Use a third temporary variable for exchanging the variables. To swap integer variables other solutions exist which do not use a third variable.

Solution:
class Program
{
static void Main(string[] args)
{
int a = 2;
int b = 3;
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine("a:{0} b:{1}", a, b);
}
}

← Back