Author : Rahul
Write a program to swap two numbers without using third number
Code:
using System;
namespace ProgrammingQuestion
{
class Program
{
static void Main(string[] args)
{
//Variable Declaration
string firstVal, secondVal;
int firstNum, secondNum;
//User Input
Console.Write("Enter First Number:");
firstVal = Console.ReadLine();
firstNum = Convert.ToInt32(firstVal);
Console.Write("Enter Second Number:");
secondVal = Console.ReadLine();
secondNum = Convert.ToInt32(secondVal);
//Swapping Logic
secondNum = firstNum + secondNum;
firstNum = secondNum - firstNum;
secondNum = secondNum - firstNum;
//Print Result
Console.WriteLine("Numbers After Swapping:");
Console.WriteLine($"First Number:{firstNum}, Second Number:{secondNum}");
Console.Read();
}
}
}
Output:
Enter First Number:5
Enter Second Number:7
Numbers After Swapping:
First Number:7, Second Number:5