أخر الاخبار

Methods في لغة C#: حلول مبتكرة لأسئلة متنوعة

 Example1 : Write C# program using method to exchange two number (Swap)?

Sol//

using System;

class Program
{
    static void Main()
    {
        int num1 = 5;
        int num2 = 10;

        Console.WriteLine($"Before swapping: num1 = {num1}, num2 = {num2}");

        // Call the Swap method to exchange the values of num1 and num2
        Swap(ref num1, ref num2);

        Console.WriteLine($"After swapping: num1 = {num1}, num2 = {num2}");
    }

    // Method to swap two numbers
    static void Swap(ref int a, ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
}


Ex2: Write C# Program using methods to read three integer number and find the value of y Where: 

Y= x1! + x2! + x3!

Sol//

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter three integer numbers:");
        int x1 = int.Parse(Console.ReadLine());
        int x2 = int.Parse(Console.ReadLine());
        int x3 = int.Parse(Console.ReadLine());

        long y = Factorial(x1) + Factorial(x2) + Factorial(x3);

        Console.WriteLine($"Y = {y}");
    }

    // Method to calculate factorial of a number
    static long Factorial(int num)
    {
        if (num == 0)
            return 1;
       
        long result = 1;
        for (int i = 1; i <= num; i++)
        {
            result *= i;
        }
        return result;
    }
}


Ex3: Write C# Program using methods to read four integer number and find the value of sum Where: Sum= x1+x2+x3+x4

Sol//

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter four integer numbers:");
        int x1 = int.Parse(Console.ReadLine());
        int x2 = int.Parse(Console.ReadLine());
        int x3 = int.Parse(Console.ReadLine());
        int x4 = int.Parse(Console.ReadLine());

        int sum = Sum(x1, x2, x3, x4);

        Console.WriteLine($"Sum = {sum}");
    }

    // Method to calculate sum of four numbers
    static int Sum(int num1, int num2, int num3, int num4)
    {
        return num1 + num2 + num3 + num4;
    }
}


Example4/ Write C# program using methods to find the average of set numbers ended by number equal first number?

Sol//

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter a set of numbers (terminate with the first number entered):");

        // Read the first number
        double firstNumber = double.Parse(Console.ReadLine());

        // Calculate the average of the set of numbers
        double average = CalculateAverage(firstNumber);

        Console.WriteLine($"The average of the set of numbers is: {average}");
    }

    // Method to calculate the average of a set of numbers
    static double CalculateAverage(double firstNumber)
    {
        double sum = firstNumber;
        int count = 1;
        double currentNumber;

        do
        {
            currentNumber = double.Parse(Console.ReadLine());
            sum += currentNumber;
            count++;
        } while (currentNumber != firstNumber);

        return sum / count;
    }
}


Example5: Write C# program to create an array (A) of 10 integer numbers and an array(B) of 10 integer numbers and find the summation of array A and array B in an array C and print array A, B and C; using methods?

Sol//

using System;

class Program
{
    static void Main()
    {
        int[] arrayA = new int[10];
        int[] arrayB = new int[10];

        // Populate arrayA
        Console.WriteLine("Enter 10 integer numbers for array A:");
        InputArrayValues(arrayA);

        // Populate arrayB
        Console.WriteLine("Enter 10 integer numbers for array B:");
        InputArrayValues(arrayB);

        // Summation of arrayA and arrayB
        int[] arrayC = SumArrays(arrayA, arrayB);

        // Print arrays
        Console.WriteLine("Array A:");
        PrintArray(arrayA);

        Console.WriteLine("Array B:");
        PrintArray(arrayB);

        Console.WriteLine("Array C (Sum of A and B):");
        PrintArray(arrayC);
    }

    // Method to input values into an array
    static void InputArrayValues(int[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            Console.Write($"Enter value {i + 1}: ");
            array[i] = int.Parse(Console.ReadLine());
        }
    }

    // Method to sum two arrays
    static int[] SumArrays(int[] arrayA, int[] arrayB)
    {
        int[] result = new int[arrayA.Length];
        for (int i = 0; i < arrayA.Length; i++)
        {
            result[i] = arrayA[i] + arrayB[i];
        }
        return result;
    }

    // Method to print an array
    static void PrintArray(int[] array)
    {
        foreach (int num in array)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }
}


Example6: Write a C# program using methods to create array named table of n integer numbers, and find the following using methods:

 1- the count the positive even numbers and count positive odd numbers .

 2- increment each number in odd position by 10 and each number in even position 

by 5. 

3- Print array . 

Sol//

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter the size of the array:");
        int n = int.Parse(Console.ReadLine());

        int[] table = new int[n];

        // Populate the array
        PopulateArray(table);

        // Count positive even and odd numbers
        int countPositiveEven = CountPositiveEven(table);
        int countPositiveOdd = CountPositiveOdd(table);

        Console.WriteLine($"Count of positive even numbers: {countPositiveEven}");
        Console.WriteLine($"Count of positive odd numbers: {countPositiveOdd}");

        // Increment numbers in odd and even positions
        IncrementNumbers(table);

        // Print the array
        Console.WriteLine("Array after incrementing:");
        PrintArray(table);
    }

    // Method to populate the array
    static void PopulateArray(int[] array)
    {
        Console.WriteLine("Enter the elements of the array:");
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }
    }

    // Method to count positive even numbers
    static int CountPositiveEven(int[] array)
    {
        int count = 0;
        foreach (int num in array)
        {
            if (num > 0 && num % 2 == 0)
            {
                count++;
            }
        }
        return count;
    }

    // Method to count positive odd numbers
    static int CountPositiveOdd(int[] array)
    {
        int count = 0;
        foreach (int num in array)
        {
            if (num > 0 && num % 2 != 0)
            {
                count++;
            }
        }
        return count;
    }

    // Method to increment numbers in odd and even positions
    static void IncrementNumbers(int[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            if (i % 2 == 0) // Even position
            {
                array[i] += 5;
            }
            else // Odd position
            {
                array[i] += 10;
            }
        }
    }

    // Method to print the array
    static void PrintArray(int[] array)
    {
        foreach (int num in array)
        {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }
}


Example7 : Write a C# program to create array table of 10 string values , and search the value elt in array and print index of string?

Sol//

using System;

class Program
{
    static void Main()
    {
        string[] table = { "apple", "banana", "orange", "grape", "mango", "pineapple", "kiwi", "strawberry", "peach", "pear" };

        Console.WriteLine("Enter the string to search:");
        string elt = Console.ReadLine();

        int index = SearchString(table, elt);

        if (index != -1)
        {
            Console.WriteLine($"The string '{elt}' is found at index {index}.");
        }
        else
        {
            Console.WriteLine($"The string '{elt}' is not found in the array.");
        }
    }

    // Method to search for a string in the array and return its index
    static int SearchString(string[] array, string value)
    {
        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] == value)
            {
                return i;
            }
        }
        return -1; // String not found
    }
}





تعليقات



حجم الخط
+
16
-
تباعد السطور
+
2
-