You are on page 1of 5

[Lab No: 09] [Computer Programming]

[Arrays]

Task No.1:
Make a program in C# to print name of Days Using Dense Array.

CODE:
string[] Days_Of_Week = { "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" };
for (int i = 0; i <= 6; i++)
{
Console.WriteLine(Days_Of_Week[i]);
}

Output:

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086


[Lab No: 09] [Computer Programming]

[Arrays]

Task No.2:
Write a program, which creates an array of 20 elements of type integer and
initializes each of the elements with a value equals to the index of the element
multiplied by 5. Print the elements to the console.

CODE:
int[] arr = new int[20];

for (int i = 0; i < arr.Length; i++)


{
arr[i] = i * 7;
Console.WriteLine(arr[i]);
}

Output:

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086


[Lab No: 09] [Computer Programming]

[Arrays]

Task No.3:
Write a program, which reads two arrays from the console and checks whether
they are equal (two arrays are equal when they are of equal length and all of
their elements, which have the same index, are equal).

CODE:
bool arraysEqual = true;

Console.Write("Enter lenght of first array: ");


int length = Int32.Parse(Console.ReadLine());

int[] arrA = new int[length];

for (int i = 0; i < arrA.Length; i++)


{
Console.Write("Enter element {0}: ", i);
arrA[i] = Int32.Parse(Console.ReadLine());
}

Console.Write("\nEnter lenght of second array: ");

if (length != Int32.Parse(Console.ReadLine())) Console.WriteLine("\nArrays


have different lengths.");
else
{
int[] arrB = new int[length];

for (int i = 0; i < arrB.Length; i++)


{
Console.Write("Enter element {0}: ", i);
arrB[i] = Int32.Parse(Console.ReadLine());
}

for (int i = 0; i < arrA.Length; i++)


{
if (arrA[i] != arrB[i])
{
Console.WriteLine("\nArrays are different.");
arraysEqual = false;
break;
}
}

if (arraysEqual) Console.WriteLine("\nArrays are the same.");


}

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086


[Lab No: 09] [Computer Programming]

[Arrays]

Output:

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086


[Lab No: 09] [Computer Programming]

[Arrays]

Task No.4:
Make a program in C# in which take 5 numbers from user and then give sum
and avg. of them. Using arrays.

CODE:
Console.WriteLine("For how many numbers you want to calculate average? ");
int numbersForAverage = int.Parse(Console.ReadLine());
double[] myArrayOfNumbers = new double[numbersForAverage];
Console.WriteLine("Input your numbers");
for (int i = 0; i < myArrayOfNumbers.Length; i++)
{
myArrayOfNumbers[i] = int.Parse(Console.ReadLine());
}
double sum = 0;
for (int j = 0; j < myArrayOfNumbers.Length; j++)
{
sum += myArrayOfNumbers[j];
}
double averageOfNumbers = sum / numbersForAverage;
Console.WriteLine("The average of your numbers is: {0}",
averageOfNumbers);
Console.ReadLine();

Output:

ABDUL QUDOOS ENROLLMENT NO 3 02-131182-086

You might also like