C Sharp Notes 3
C Sharp Notes 3
1. if statement
2. Multiple if statement
3. Nested if statement
4. switch case statement
5. Tenary operator
Loops
1. for loop
2. while loop
3. do..while loop
4. Nested loop
5. Foreach … loop
ARRAYS
Arrays are used to store number of items of a predefined type. All items in a single dimension array
are stored contiguously starting from 0 to the size of the array.
The following code declares an integer array that can store 3 items.
Array declarations in C# are pretty simple. You put array items in curly braces ({}). If an array is not
initialized, its items are automatically initialized to the default initial value for the array type at the
time it is declared.
The following code declares and initializes an array of three items of integer type.
You can even directly assign these values without using the new operator.
A multi-dimensional array, also known as a rectangular array is an array with more than one
dimension. The form of a multi-dimensional array is a matrix.
string[,] mutliDimStringArray;
The following code snippet is an example of fixed-sized multi-dimensional arrays that defines two
multi dimension arrays with a matrix of 3x2 and 2x2. The first array can store 6 items and second
array can store 4 items. Both of these arrays are initialized during the declaration.
Now let's see examples of multi-dimensional dynamic arrays where you are not sure of the number of
items of the array. The following code snippet creates two multi-dimensional arrays with no limit.
You can also omit the new operator as we did in single dimension arrays. You can assign these values
directly without using the new operator. For example:
int[,] numbers = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
We can also initialize the array items one item at a time. The following code snippet is an example of
initializing array items one at a time.
numbers[0, 0] = 1;
numbers[1, 0] = 2;
numbers[2, 0] = 3;
numbers[0, 1] = 4;
numbers[1, 1] = 5;
numbers[2, 1] = 6;
A multi-dimensional array items are represented in a matrix format and to access it's items, we need to
specify the matrix dimension. For example, item(1,2) represents an array item in the matrix at second
row and third column.
The following code snippet shows how to access numbers array defined in the above code.
Console.WriteLine(numbers[0,0]);
Console.WriteLine(numbers[0, 1]);
Console.WriteLine(numbers[1, 0]);
Console.WriteLine(numbers[1, 1]);
Console.WriteLine(numbers[2, 0]);
Console.WriteLine(numbers[2, 2]);
CONDITIONS
1. Accept the year from user and say whether it is a leap year. (Check all 3 condition : 2013
Not, , 2016 Leap, 2100 Not, 2400 Leap)
2. Accept an alphabet and say whether it’s vowel or not (using SWITCH..CASE).
3. Accept 3 numbers and using conditional statement display the maximum number. (Use &&)
5. Considering following selection criteria, display whether the candidate is SELECTED or NOT
SELECTED.
MALE:-
NOTE:
In both of above cases 1 year less experience is considered in case the candidate has a master degree.
LOOPS
2. Accept a number of 4 digit. Find reverse of that number. Multiply the reverse number with a
separately accepted integer value and display the result.
In case of circle user should be asked to enter RADIUS, and then ask whether to find AREA /
DIAMETER and then calculate and display.
In case of cylinder, user should be asked whether BASE AREA / VOLUME. If base area then ask
radius, and if volume then ask radius and height to enter. Calculate and display the same as per
option.
After displaying above result ask user whether any more SHAPE to check? If "yes" then repeat the
above process.
4. Accept a 3 digit number and check whether that number is an armstrong number?
(An Armstrong number is that 3 digit number whose sum of cube of each digit is equal to that
number)
ARRAY
Q1. Accept an integer array of 6 elements and then accept another number to check whether the
number exists in the array or not.
Q4: User should enter a number “n” indicating the array should n X n. Accept the value into
that n X n array. Find sum of left diagonal elements in it.