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


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2.  
  3. public class functionexcercise
  4. {
  5. public static void interchangenumber(ref int num1, ref int num2)
  6. {
  7. int newnum;
  8.  
  9. newnum = num1;
  10. num1 = num2;
  11. num2 = newnum;
  12. }
  13. public static void Main()
  14. {
  15. int n1, n2;
  16. Console.Write("Enter two number: ");
  17. n1 = Convert.ToInt32(Console.ReadLine());
  18. n2 = Convert.ToInt32(Console.ReadLine());
  19. interchangenumber(ref n1, ref n2);
  20. Console.WriteLine("After swapping the 1st number is : {0} , and the 2nd number is : {1}", n1, n2);
  21.  
  22. Console.ReadLine();
  23. }
  24. }

Result

C# language techstudy

     





Related Topics




Popular Posts