.NET Practical Slip 22 Q A

Q. Write a program in C# to create a function to swap the values of two integers. 15 Marks

using System;

class Program
{
    // Function to swap two integers
    static void Swap(ref int a, ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }

    static void Main()
    {
        int num1 = 10;
        int num2 = 20;

        Console.WriteLine("Before Swapping:");
        Console.WriteLine("num1 = " + num1);
        Console.WriteLine("num2 = " + num2);

        Swap(ref num1, ref num2);

        Console.WriteLine("After Swapping:");
        Console.WriteLine("num1 = " + num1);
        Console.WriteLine("num2 = " + num2);

        Console.ReadLine();
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top