Q:

Write a program in C# Sharp to create a function to swap the values of two integer numbers

0

Write a program in C# Sharp to create a function to swap the values of two integer numbers.

Test Data :
Enter a number: 5
Enter another number: 6
Expected Output :
Now the 1st number is : 6 , and the 2nd number is : 5

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
public class funcexer6
{
    public static void interchange(ref int num1, ref int num2)
    {
        int newnum;        
        newnum = num1;
        num1 = num2;
        num2 = newnum;
    }
    public static void Main()
    {
      int n1,n2;
	  Console.Write("\n\nFunction : To swap the values of two integer numbers :\n");
      Console.Write("----------------------------------------------------------\n");
	  Console.Write("Enter a number: ");
      n1= Convert.ToInt32(Console.ReadLine());
      Console.Write("Enter another number: ");
      n2= Convert.ToInt32(Console.ReadLine());		
      interchange( ref n1, ref n2 );
      Console.WriteLine( "Now the 1st number is : {0} , and the 2nd number is : {1}", n1, n2 );
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now