حل اسئلة امتحان اساسيات البرمجة - قسم الحاسوب - الجامعة المستنصرية

codinglabsolution

 

Q1/ Choose the most suitable answer: (16 Marks)

1- Which of the following statement is correct?

a- char x='s' b- char x="s"; c- char x= s; d- char x='s';


2- What is the size of double?

a- 8 bytes b- 4 bytes c- 32 bytes d- 16 bytes


3- What is the output after the following sequence of statements is executed?

int x=4 , y=2 , z;

z=x+y;

Console.WriteLine("x={1} y= {0} z={2}",x,y,z);

a- x=6 y = 2 z= 4 b- x=2 y = 6 z= 4 c- x=4 y= 2 z= 6 d- x=2 y= 4 z= 6


4- Which of the following is Not relational operators in C#.NET?

a- >= b- <= c- < >= d- *


5- The following code is used to print the ---------- :

int num;

if (num % 2 == 0)

if (num < 0)

Console.WriteLine("num " + num);


a- Even positive number b- Even negative number c- Odd positive number

d- Odd negative number e- None of the answers.


6- What is the output after the following sequence of statements is executed?

(Assume that : x = 9 , y= 2)

if (x > 5)

if (y > 5)

Console.Write ("first \t");

else

Console.Write("second \t ");

Console.WriteLine("end");

a- first b- first end c- second end d- second


7- The following box denotes?




a) Decision

b) Initiation

c) Initialization

d) I/O


8- The default case is required in the --------------- structure.

a- switch b- if – else c- for statement d- while statement


Q2/ (Answer only one) (7 Marks)

A) Write a C# program to find the value of z using switch statement :

z = {

2xy + x  if i= 1

3xy − x  if i= 2

4xy/x     if i= 3

5xy ∗ x  if i= 4

}

Sol//

using System;

class FindZValue
{
    static void Main(string[] args)
    {
        int x, y, i;

        // Get input values from the user
        Console.Write("Enter the value of x: ");
        x = int.Parse(Console.ReadLine());

        Console.Write("Enter the value of y: ");
        y = int.Parse(Console.ReadLine());

        Console.Write("Enter the value of i (1, 2, 3, or 4): ");
        i = int.Parse(Console.ReadLine());

        double z; // Use double for potentially floating-point results

        switch (i)
        {
            case 1:
                z = 2 * x * y + x;
                break;
            case 2:
                z = 3 * x * y - x;
                break;
            case 3:
                // Check for division by zero
                if (x == 0)
                {
                    Console.WriteLine("Error: Division by zero is not allowed.");
                    return; // Exit the program if division by zero
                }
                z = (double)4 * x * y / x; // Cast to double for clarity
                break;
            case 4:
                z = 5 * x * y * x;
                break;
            default:
                Console.WriteLine("Invalid value for i. Please enter 1, 2, 3, or 4.");
                break;
        }

        Console.WriteLine("The value of z is: {0}", z);
    }
}

B) Draw a flowchart and write C# program to calculate area for rectangle and test if the area is greater than 50 or not?

Sol//

Flowchart for Rectangle Area Calculation

+--------------------+
|      Start         |
+--------------------+
          |
          V
+--------------------+
|  Enter length (L)   |
+--------------------+
          |
          V
+--------------------+
|  Enter width (W)    |
+--------------------+
          |
          V
+--------------------+
|  Area = L * W       |
+--------------------+
          |
          V
+--------------------+
|  Area > 50? (Yes/No) |
+--------------------+
          |         Yes
          V         |
+--------------------+
|  Print "Area > 50"  |
+--------------------+
          |         No
          V         |
+--------------------+
|  Print "Area <= 50" |
+--------------------+
          |
          V
+--------------------+
|       End          |
+--------------------+

C# Program for Rectangle Area Calculation

using System;

class RectangleArea
{
    static void Main(string[] args)
    {
        double length, width, area;

        // Get input for length and width
        Console.Write("Enter the length of the rectangle: ");
        length = double.Parse(Console.ReadLine());

        Console.Write("Enter the width of the rectangle: ");
        width = double.Parse(Console.ReadLine());

        // Calculate area
        area = length * width;

        // Check if area is greater than 50
        if (area > 50)
        {
            Console.WriteLine("The area of the rectangle is greater than 50.");
        }
        else
        {
            Console.WriteLine("The area of the rectangle is not greater than 50.");
        }
    }
}


Q3/ (Answer only two) (7 Marks)

Q) Write C# program to find the value of z using the formula: using if-else
Q) Write C# program to find the value of z using the formula: using do-while

Sol//

using System;

public class FindZValue
{
    public static void Main(string[] args)
    {
        int x, y, i;
        double z; // Use double for floating-point calculations

        Console.Write("Enter the value of x: ");
        x = int.Parse(Console.ReadLine());

        Console.Write("Enter the value of y: ");
        y = int.Parse(Console.ReadLine());

        Console.Write("Enter the value of i (1, 2, or 3): ");
        i = int.Parse(Console.ReadLine());

        if (i == 1)
        {
            z = Math.Sqrt(Math.Pow(x, 2) * Math.Pow(y, 2) + x * y + 5);
        }
        else if (i == 2)
        {
            z = Math.Abs(3 * Math.Pow(x, 2) - 2 * Math.Pow(y, 2) - 5);
        }
        else if (i == 3)
        {
            if (x == 0)
            {
                Console.WriteLine("Error: Division by zero is not allowed.");
                return;
            }
            z = 4.0 * x * y / x; // Cast x to double for division accuracy
        }
        else
        {
            Console.WriteLine("Invalid value for i. Please enter 1, 2, or 3.");
            return;
        }

        Console.WriteLine("The value of z is: " + z);
    }
}

B) Draw a flowchart to read two numbers x and y , and swap the two numbers if x divided by 4 .

Sol//


+----------------+
| Start           |
+----------------+
         v
+----------------+
| Read number x   |
+----------------+
         v
+----------------+
| Read number y   |
+----------------+
         v
+----------------+  +-----------------------+
| Is x divisible  |  | Yes (Swap)             |
| by 4 (x % 4==0)? | ----> | Swap temporary = x;  |
+----------------+         |  x = y;                |
         v                 |  y = temporary;       |
+----------------+         +-----------------------+
         v                 v
+----------------+         +----------------+
|        No       | ----> | Print x and y   |
+----------------+         | (unchanged)      |
         v                 +----------------+
+----------------+         | End             |
| Print x and y   |         v
+----------------+         +----------------+

C) Declare several variables by selecting for each one of them the most appropriate of the types: 52, -115, 45.506, ‘z’, 1.4, “hello”

Sol//

1. 52: `int` (integer)

2. -115: `int` (integer)

3. 45.506: `double` (floating-point number)

4. 'z': `char` (character)

5. 1.4: `double` (floating-point number)

6. "hello": `string` (sequence of characters)

So, the declaration for these variables would be:

int num1 = 52;
int num2 = -115;
double num3 = 45.506;
char character = 'z';
double num4 = 1.4;
string message = "hello";


(الجزء العملي) Practical

Note: Answer only one question (12 Marks)

Q1/ Write a C# program to read two integer numbers (a, b) and swap the two numbers if a and b are odd and a is divided by 5.

Sol//

using System;

class SwapOddDivisibleByFive
{
    static void Main(string[] args)
    {
        int a, b;

        // Get input for a and b
        Console.Write("Enter the first number (a): ");
        a = int.Parse(Console.ReadLine());

        Console.Write("Enter the second number (b): ");
        b = int.Parse(Console.ReadLine());

        // Check if both a and b are odd and a is divisible by 5
        if (a % 2 != 0 && b % 2 != 0 && a % 5 == 0)
        {
            // Swap a and b
            int temp = a;
            a = b;
            b = temp;

            Console.WriteLine("After swapping (both odd, a divisible by 5):");
            Console.WriteLine($"a = {a}");
            Console.WriteLine($"b = {b}");
        }
        else
        {
            Console.WriteLine("Numbers not swapped (conditions not met).");
            Console.WriteLine($"Original a = {a}");
            Console.WriteLine($"Original b = {b}");
        }
    }
}

Q2 / Write a C# program to read the student’s name, gender, three scores and find the average of the scores then print “pass” when the average is greater than 50, otherwise print “failed”.

Sol//

using System;

class StudentResult
{
    static void Main(string[] args)
    {
        string name, gender;
        double score1, score2, score3, average;

        // Get student information
        Console.Write("Enter student's name: ");
        name = Console.ReadLine();

        Console.Write("Enter student's gender (M/F): ");
        gender = Console.ReadLine().ToUpper(); // Convert input to uppercase for easier comparison

        // Get scores
        Console.Write("Enter score 1: ");
        score1 = double.Parse(Console.ReadLine());

        Console.Write("Enter score 2: ");
        score2 = double.Parse(Console.ReadLine());

        Console.Write("Enter score 3: ");
        score3 = double.Parse(Console.ReadLine());

        // Calculate average
        average = (score1 + score2 + score3) / 3;

        // Determine and print result
        string result = average > 50 ? "pass" : "failed";
        Console.WriteLine($"{name} ({gender}) - Average: {average:F2} - Result: {result}");
    }
}



Q1/ Choose the most suitable answer: 

1-This Flowchart represents the flow control of ------- statement:

do-while


 a- do-While b- while c- for d- if e- if – else 



 2- Choose a correct C# for loop sysntax :

 a- for(declaration; incrementoperation ; condition) { //statements }

 b- for(initialization , condition , incrementoperation , ) { //statements }

 c- for(initialization ; condition ; incrementoperation ) { //statements }

 d- for(initialization ; incrementoperation ; condition) { //statements } 

3- Which loop is guaranteed to execute at least one time. 

a- for b- do while c- while d- None of the above 


Q2\ What is the output after the following sequence of statements is executed? 

I'd be glad to explain the output of the given C# code:

C#
int z = 9;
do {
  Console.Write(z + "\t ");
  Console.WriteLine(2 * z);
  z = z - 2;
} while (z >= 0);

Output:

9    18
7    14
5    10
3    6
1    2

Q3/ Answer the following: (Answer one only) 

A) Write a C# program to find the value of Y? .


Sol//

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the value of X:");
        double x = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Enter the value of n:");
        int n = Convert.ToInt32(Console.ReadLine());

        double y = 5;

        for (int i = 1; i <= n; i++)
        {
            if (i == 1)
            {
                y += (2 * Math.Pow(x, i)) / i;
            }
            else if (i == 2)
            {
                y += (4 * Math.Pow(x, i)) / i;
            }
            else
            {
                y += ((i + 1) * Math.Pow(x, i)) / i;
            }
        }

        Console.WriteLine($"The value of Y is: {y}");
    }
}


B) Write C# program to find the average of set numbers ended by number divided by 3?

Sol//

using System;

class Program
{
    static void Main(string[] args)
    {
        int sum = 0;
        int count = 0;

        Console.WriteLine("Enter numbers (end with a number divisible by 3):");

        int num;
        do
        {
            num = Convert.ToInt32(Console.ReadLine());

            // Check if the number is divisible by 3
            if (num % 3 == 0)
            {
                break; // Exit the loop if divisible by 3
            }

            sum += num;
            count++;
        } while (true);

        // Calculate and display the average
        if (count > 0)
        {
            double average = (double)sum / count;
            Console.WriteLine($"Average of the numbers: {average}");
        }
        else
        {
            Console.WriteLine("No numbers entered.");
        }
    }
}



قوانين المصفوفة المربعة


 main diagonal ( i == j) 

triangle upper main diagonal ( i < j) 

triangle lower main diagonal. ( i> j ) 

secondary diagonal. ( i+j == GetLength(0) -1) 

 ex ( i+ j == 2)

elements on main diagonal a[i,i] 

elements on secondary diagonal. a[ i, GetLength(0) -1-i] 

 a[i,2-i]


ex1 : Write a C# program to read and print an array of two dimension [3X3] of integer numbers and print and sum of the elements on triangle upper main diagonal . 

ننتبه على صيغة السؤال هنا طباعة العناصر فوق القطرية مع ايجاد حاصل جمع هذه العناصر

Sol//

using System;

class UpperDiagonalSum
{
    static void Main(string[] args)
    {
        int[,] numbers = new int[3, 3];

        // Read array elements
        Console.WriteLine("Enter the elements of the 3x3 array:");
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                numbers[i, j] = int.Parse(Console.ReadLine());
            }
        }

        // Print the array
        Console.WriteLine("The array:");
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write(numbers[i, j] + " ");
            }
            Console.WriteLine();
        }

        // Calculate and print the sum of upper triangle main diagonal
        int sum = 0;
        for (int i = 0; i < 3; i++)
        {
            // Include the main diagonal element (i == j)
            sum += numbers[i, i];
        }

        Console.WriteLine("Sum of elements on the upper triangle main diagonal: {0}", sum);
    }
}


Ex2 : Write a C# program to read and print an array of two dimension [3X3] of integer numbers and print the elements of row1

ننتبه على صيغة السؤال هنا طباعة عناصر السطر الأول بالمصفوفة

Sol//

using System;

class PrintRow1
{
    static void Main(string[] args)
    {
        int[,] numbers = new int[3, 3];

        // Read array elements
        Console.WriteLine("Enter the elements of the 3x3 array:");
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                numbers[i, j] = int.Parse(Console.ReadLine());
            }
        }

        // Print elements of row 1
        Console.WriteLine("Elements of row 1:");
        for (int j = 0; j < 3; j++)
        {
            Console.Write(numbers[0, j] + " "); // Access elements in row 0 (first row)
        }
        Console.WriteLine();
    }
}


Group A

 Q1) Choose the correct answer :

 1-What is the output after the following sequence of statements is executed?

 double z ;

int x=15 , y=2;
z = x / y ;
Console.WriteLine("x= {1} y= {0} z= {2}" , x , y , z);

A- x= 2 y = 15 z= 7.5 

B- x=2 y = 15 z= 7

 C- x=15 y= 2 z= 7 

D- x=15 y= 2 z= 7.5 

2- What is the size of short type ?

 A- 2 bytes B- 4 bytes C - 8 bytes D- 16 bytes 

3- Secondary storage is usually volatile, which means that it is erased when the machine is powered off.

A- TRUE B- FALSE 

4-The following box denotes? 



A- Input/Output B- start / end C- Decision D- Process 

5- What is the output after the following sequence of statements is executed? 

double x= 6 , y=2, z;

 z = 2*y +x / y +x ;

 Console.WriteLine("z= {0}" , z);

 A- z= 11 B- z= 12 C- z= 10 D- z= 13 

6-What is the output of the following codes : 

 double y = Math.Ceiling(-4.3 );

 Console.WriteLine ( " y= " + y);

A- y= -5 B- y= -4 C- y= -4.5 D- y= 4 

7- Example of valid identifier is :

 A- Sum 2 B- Sum-2 C- 2Sum D- Sum2


Q2\ Answer the following: A\ Write algorithm and draw a flowchart to read the student’s name and three scores and find the average of the scores then print “pass” when the average is greater than 50, otherwise print “failed”.

Sol//

Algorithm : find average 

Input : name , three score 

Output : find average and print pass or failed

 Step1 : start 

Step2 : read name , degree1 , degree2, degree3 

Step3 : average = (degree1+degree2+ degree3)/3 

Step 4: print name and average 

Step5 : if average >= 50 print “pass” Else print “failed”

 Step 6: end 

flowchart to read the student


B\ write C# program to find z of the following : 


Sol//

static void Main(string[] args)
 {
 double x , y, z;
 Console.Write("input x :");
 x = double.Parse(Console.ReadLine());
 y = 3 * x;

 z = Math.Sqrt(x * x * y * y) +2 * x * y;
 Console.WriteLine("y= {0} z= {1} ", y , z );
 Console.ReadKey();
 }


Q3\Answer the following : 

A\ Write the following equation in C# language and indicate the order of evaluation of the operators in the C# statement . 


B/ List the logical units of computer? ( Computer Organization ) 

1. Input unit. 

2. Output unit.

 3. Memory unit. 

4. Central processing unit (CPU). The CPU serves as the “administrative” section of the computer. This is the computer’s coordinator, responsible for supervising the operation of the other sections. The CPU has two components: 

 Control Unit: extracts instructions from memory and decodes and executes them 

 Arithmetic Logic Unit (ALU): handles arithmetic and logical operations 

5. Secondary storage unit. 


Group B 

Q1) Choose the correct answer : 

1-What is the output of the following codes : 

 double y = Math.Floor( -2.6 );


 Console.WriteLine ( " y= " + y);

A- y= -3 B- y= -2 C- y= 3 D- y= -2.5 

2- Example of valid identifier is : 

A- Area 2 B- 2Area C- Area2 D- Area-2 

3- Which of the following statement is correct?

 A- string a="Ahmed " B- string a="Ahmed " ; C- string a=Ahmed ; D- string a= 'Ahmed '; 

4-The following box denotes? 

4-The following box denotes?


A- Decision B- start / end C- Process D- Input/Output 

5-What is the output after the following sequence of statements is executed? 

int x=5 , y=3 , z ;

z = x + y ;

Console.WriteLine("x= {2} y= {0} z= {1}" , x , y , z);

A- x= 8 y = 3 z= 5 B- x=5 y = 3 z= 8 C- x=8 y= 5 z= 3 D- x=3 y= 8 z= 5 

6- What is the size of float type ?

 A- 2 bytes B- 4 bytes C - 8 bytes D- 16 bytes 

7- What is the output after the following sequence of statements is executed? 

double x= 2 , y=4 , z;

z = 2*y / x + 2*x + x ;

Console.WriteLine("z= {0}" , z);

A- z= 10 B- z= 12 C- z= 6 D- z= 4

Q2\ Answer the following : 

A\ Write algorithm and draw a flowchart to read two numbers x and y, and swap the two numbers if 

x +y is even number ?

 Algorithm : swap two number if x+y is even number

 Input : two number x and y 

Output : print x and y after swap

 Step1 : start 

Step2 : read x , y

 Step3 : if x+y mod 2 equal 0 then t= x ; x=y ; y = t; (swap two number ) 

Step 4 : print x , y 

 Step 5: end  

Algorithm : swap two number if x+y is even number


B\ Write C# program to find z of the following : 

B\ Write C# program to find z of the following :

Sol//

static void Main(string[] args)
 {
 double a, b, z;
 Console.Write("input a :");
 a = double.Parse(Console.ReadLine());
 b = Math.Sin(a);
 z = (2 * a + 5) / (3 * b - 1);
 Console.WriteLine("b= {0} z= {1} ", b , z );
 Console.ReadKey();
 }


Q3\ Answer the following : 

A\ Write the following equation in C# language and indicate the order of evaluation of the operators in the C# statement . 

A\ Write the following equation in C# language and indicate the order of evaluation of the operators in the C# statement .


B\ List the levels of programming Languages ? 

Sol//

 1. Machine languages 

 2. Assembly languages 

 3. High-level languages


Q1/ Choose the most suitable answer:
1- If an array has 10 items, the last 10th item is at ------- position.
 a) 11th
 b) 9th
 c) 8 th
 d) 10th

2- Arrays in C# are ------------------- objects.
 a) Logical b) Value c) Reference d) Arithmetic e) None

3- Assum: int [ ] t= {1,9,6,3,2}; What is t.Length ?
 a) 5 b) 4 c) 6 d) 0

4- What is the output of the following code ?
 int S = 0;
 int[,] A = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
 for (int i = 0; i < A.GetLength(0); i++)
 for (int j = 0; j < A.GetLength(1); j++)
 if (i + j = = 2)
 S = S + A[i, j];
 Console.Write( S );
 a) 16 b) 15 c) 19 d) 23 e) 25

5- Which of the following statement is correct?
a) int [ ] a = new a[8];
b) int a [ ] = new int [8];
c) int a [8 ] = new int [ ];
d) int [ ] a = new int [8];

Q2/ Write a C# program to read and print an array of two dimension
[4X4] of integer numbers and find the following : (Choose only two)
1- Exchange Row0 With Row 2.
2- The sum of elements on triangle lower main diagonal.
3- The sum of the elements in each odd rows.

static void Main(string[] args)
 {
 int[,] a = new int [4,4];
 for (int i = 0; i < a.GetLength(0); i++)
 for (int j = 0; j < a.GetLength(1); j++)
 {
 Console.Write("Enter array value: ");
 a[i, j] = Int32.Parse(Console.ReadLine());
 }
 //1- Exchange Row0 With Row 2.
 int t;
 for (int j = 0; j < a.GetLength(1); j++)
 {
 t = a[0, j];
 a[0, j] = a[2, j];
 a[2, j] = t;
 }
 //2- The sum of elements on triangle lower main diagonal.
 int sum = 0;
 for (int i = 0; i < a.GetLength(0); i++)
 for (int j = 0; j < a.GetLength(1); j++)
 if ( i>j)
 sum=sum+a[i,j];
 Console.WriteLine("The sum of elements on triangle lower main diagonal : " + sum);
 //3- The sum of the elements in each odd rows.
 int sum1 = 0;
 for (int i = 1; i < a.GetLength(0); i += 2)
 {
 for (int j = 0; j < a.GetLength(1); j++)
 sum1 = sum1 + a[i, j];
 Console.WriteLine("The sum of elements on Row {0} is {1} " , i ,sum1);
 }
 //Printing the array
 for (int i = 0; i < a.GetLength(0); i++)
 {
 for (int j = 0; j < a.GetLength(1); j++)
 {
 Console.Write("\t{0}",a[i, j]);
 }
 Console.WriteLine();
 }
 Console.ReadKey();
}


Q3/ Write a C# program to read and print one dimension array (A)
of 10 integer numbers, and find the following:
(Choose only two)
1- The count of the numbers divisible by 7 in the array.
2- Search about value entered by the user in the array and print the index .
3- Increment 3 for each number greater than or equal to 30, and increment 5 for
each number less than 30.

static void Main(string[] args)
 {

 int[] a = new int[10];
 for (int i = 0; i < a.Length; i++)
 {
 Console.Write("Enter array value: ");
 a[i] = Int32.Parse(Console.ReadLine());
 }
 //1- 1- The count of the numbers divisible by 7 in the array.
 int c = 0;
 for (int i = 0; i < a.Length; i++)
 if (a[i] % 7 == 0 )
 c++;
 Console.WriteLine("1- The count of the numbers divisible by 7 in the array. " + c);
 //2- Search about value entered by the user in the array and print the index .
 int value ;
 Console.Write("input value : ");
 value=int.Parse(Console.ReadLine());
 int k;
 for ( k= 0; k < a.Length; k++)
 if (a[k] == value )
 break;
 if (k!= a.Length)
 Console.WriteLine("The value is found in index " + k);
 else
 Console.WriteLine("The value not found " );
 //3- Increment 3 for each number greater than or equal to 30, and increment 5 for each
number less than 30.
 for (int i = 0; i < a.Length; i++)
 if (a[i] >= 30 )
 a[i] =a[i] + 3;
 else
 a[i]= a[i]+ 5;
 //Printing the array a
for (int i = 0; i < a.Length; i++)
 Console.Write(a[i]+" ");
 Console.ReadKey();
 }
 }


Q1/ Choose the most suitable answer:
1- What is the output of the following C# segment of code:
 int[ ,] A = {{1,2} ,{3,4 }, {5,6 }};
 Console.WriteLine ( A.GetLength(0));

 a) 2 b) 3 c) 6 d) 5

2- What will be the contents of array a [5 ] after executing the following code:
 int[ ] a = new int [5] ;
 for (int i = 0; i < a.Length ; i++)
 a[i]= i*3;

 a) 1 2 3 4 5 b) 0 3 6 9 12 15 c) 0 3 6 9 12 d) 3 6 9 12

3- If an array has 8 items, the last 8th item is at ------- position.
 a) 9th
 b) 8th
 c) 6th
 d) 7th

4- Arrays in C# are ------------------- objects.
 a) Reference b) Value c) Logical d) Arithmetic e) None

5- Which of this following C# segment of code is correct ?
 a)                         b)                            c)
 int[] a = {5,10,15,20} ; int[] a = {5,10,15,20} ; int[] a = {5,10,15,20} ;
 foreach(int i in a) foreach(int a in i) foreach(int i in a)
 Console.Write(" " + a[i]); Console.Write(" " + i); Console.Write(" " + i);

 d)                                    e)
 int[] a = {5,10,15,20} ; int[] a = {5,10,15,20} ;
 foreach(int i in a) foreach(int a in i)
 Console.Write(" " + a); Console.Write(" " + a[i]);

Q2/ Write a C# program to read and print an array of two dimension
[5X5] of integer numbers and find the following : (Choose only two)
1- The count of even positive number in the array .
2- The sum of elements on main diagonal.
3- Convert the all numbers in two dimensional array into one dimensional array.

static void Main(string[] args)
 {
 int[,] a = new int[5, 5];
 int[] b = new int[25];
 for (int i = 0; i < a.GetLength(0); i++)
 for (int j = 0; j < a.GetLength(1); j++)
 {
 Console.Write("Enter array value: ");
 a[i, j] = Int32.Parse(Console.ReadLine());
 }
 //1- The count of even positive number in the array .
 int c = 0;
 for (int i = 0; i < a.GetLength(0); i++)
 for (int j = 0; j < a.GetLength(1); j++)
 if (a[i, j] % 2 == 0 && a[i, j] >= 0)
 c++;
 Console.WriteLine("The count of even positive number in the array . " + c);
 //2- The sum of elements on main diagonal.
 int sum = 0;
 for (int i = 0; i < a.GetLength(0); i++)
 for (int j = 0; j < a.GetLength(1); j++)
 if (i == j)
 sum = sum + a[i, j];
 Console.WriteLine("The sum of elements on main diagonal: " + sum);
 //3-Convert the all numbers in two dimensional array into one dimensional array.
 int k = 0;
 for (int i = 0; i < a.GetLength(0); i ++)

 for (int j = 0; j < a.GetLength(1); j++)
 {
 b[k] = a[i, j];
k++;
 }
 //Printing the array a
 for (int i = 0; i < a.GetLength(0); i++)
 {
 for (int j = 0; j < a.GetLength(1); j++)

 Console.Write("\t{0}", a[i, j]);
 Console.WriteLine();
 }
 //Printing the array b
for (int i = 0; i < k; i++)
 Console.Write(b[i]+" ");
 Console.ReadKey();
 }
 }

Q3/ Write a C# program to read and print one dimension array (A) of 10
integer numbers, and find the following:(Choose only two)
1- The sum of the numbers divisible by 3 in the array.
2- Exchange the first element with the last one.
3- Sort the array

static void Main(string[] args)
 {

 int[] a = new int[10];
 for (int i = 0; i < a.Length; i++)
 {
 Console.Write("Enter array value: ");
 a[i] = Int32.Parse(Console.ReadLine());
 }
 //1- The sum of the numbers divisible by 3 in the array.
 int s = 0;
 for (int i = 0; i < a.Length; i++)
 if (a[i] % 3 == 0)
 s=s+a[i];
 Console.WriteLine("1- The sum of the numbers divisible by 3 in the array. " + s);
 //2- Exchange the first element with the last one.
 int t;
 t= a[0];
 a[0]=a[9];
 a[9]=t;
 //Printing the array before sort a
 for (int i = 0; i < a.Length; i++)
 Console.WriteLine(a[i] );
 //3- sort array

 for (int pass = 0; pass < a.Length; pass++)
 for (int i = 0; i < a.Length - 1; i++)
 if (a[i] > a[i + 1])
{
 t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
 }
 //Printing the array after sort
 for (int i = 0; i < a.Length; i++)
 Console.WriteLine(a[i] );
 Console.ReadKey();
 }




إرسال تعليق

ملفات تعريف الارتباط
نستخدم ملفات تعريف الارتباط (Cookies) لفهم كيفية استخدامك لموقعنا وتحسين تجربتك في المحتوى والإعلانات. باستمرارك في تصفح الموقع، فإنك توافق على استخدامنا لملفات تعريف الارتباط وفقًا لسياسة الخصوصية لدينا.
أُووبس!
يبدو أن هناك خطأ ما في اتصالك بالإنترنت. يرجى الاتصال بالإنترنت وبدء التصفح مرة أخرى.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.