0% found this document useful (0 votes)
19 views

C Sharp Notes 3

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C Sharp Notes 3

Uploaded by

rupams2024
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Conditional Statement

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.

int[] intArray = new int[3];

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.

int[] staticIntArray = new int[3] {1, 3, 5};

The following code declares and initializes an array of 5 string items.

string[] strArray = new string[5] { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };

You can even directly assign these values without using the new operator.

string[] strArray = { "Mahesh", "Mike", "Raj", "Praveen", "Dinesh" };


Multi-Dimensional Arrays

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.

A multi dimension array is declared as following:

string[,] mutliDimStringArray;

A multi-dimensional array can be fixed-sized or dynamic sized.

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.

int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

string[,] names = new string[2, 2] { { "Rosy", "Amy" }, { "Peter", "Albert" } };

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.

int[,] numbers = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

string[,] names = new string[,] { { "Rosy", "Amy" }, { "Peter", "Albert" } };

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 } };

string[,] names = { { "Rosy", "Amy" }, { "Peter", "Albert" } };

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.

int[,] numbers = new int[3, 2];

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]);

Assignment Questions on Conditions, Loops and Array

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 &&)

4. Using ternary operator condition find maximum among 3 integer values.

5. Considering following selection criteria, display whether the candidate is SELECTED or NOT
SELECTED.

If not selected display at least 1 reason of not being selected.

MALE:-

o Should have a degree


o Age should be between 18 to 35
o Experience minimum 5 years
o 1 year less experience is considered in case the candidate has a master degree
FEMALE:-

o Should have a degree


o Age should be between 20 to 35
o Experience minimum 4 years

NOTE:

In both of above cases 1 year less experience is considered in case the candidate has a master degree.

LOOPS

1. Display first 15 elements of Fibonacci series. ( 0,1,1,2,3,5,8,13, . . . . )

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.

3. Using switch case solve the following:

Accept the name of SHAPE (option: 1 for Circle, 2 for Cylinder).

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.

Q 2. Accept a 3 X 3 Matrix and display TRANSPOSE Matrix.


Q3. Accept an integer array of 5 elements and sort it in ascending order.

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.

You might also like