Write a C# Sharp program to swap two numbers

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to swap two numbers

Sample Output:

Input the First Number : 2    
Input the Second Number : 5    
After Swapping :                                                                                              
First Number : 5                                                                                              
Second Number : 2 

The act of swapping two variables refers to mutually exchanging the values of the variables. Generall, this is done with the data in memory.

Using a temporary variable :

The simplest method to swap two variables is to use a third temporary variable :

define swap(x, y)
    temp := x
    x := y
    y := temp

الأجوبة

using System;
public class Exercise5
{
       public static void Main(string[] args)
         {
            int number1, number2, temp;
            Console.Write("\nInput the First Number : ");
            number1 = int.Parse(Console.ReadLine());
            Console.Write("\nInput the Second Number : ");
            number2 = int.Parse(Console.ReadLine());
            temp = number1;
            number1 = number2;
            number2 = temp;
            Console.Write("\nAfter Swapping : ");
            Console.Write("\nFirst Number : "+number1);
            Console.Write("\nSecond Number : "+number2);
            Console.Read();
        }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...