Step 1:- Method
public void PrintSwapTwoNumber1()
{
Console.WriteLine("Enter
first number");
int Num1
= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter
second number");
int Num2
= Convert.ToInt32(Console.ReadLine());
int temp;
Console.WriteLine("Before
Swap\n");
Console.WriteLine("Num1
Values: {0}", +Num1);
Console.WriteLine("Num2
Values: {0}", +Num2);
Console.WriteLine("");
temp
= Num1;
Num1
= Num2;
Num2
= temp;
Console.WriteLine("After
Swap\n");
Console.WriteLine("Num1
Values: {0}", +Num1);
Console.WriteLine("Num2
Values: {0}", +Num2);
}
public void PrintSwapTwoNumber2()
{
Console.WriteLine("Enter
first number");
int Num1
= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter
second number");
int Num2
= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Before
Swap\n");
Console.WriteLine("Num1
Values: {0}", +Num1);
Console.WriteLine("Num2
Values: {0}", +Num2);
Console.WriteLine("");
Num1
= Num1 + Num2;
Num2
= Num1 - Num2;
Num1
= Num1 - Num2;
Console.WriteLine("After
Swap\n");
Console.WriteLine("Num1
Values: {0}", +Num1);
Console.WriteLine("Num2
Values: {0}", +Num2);
}
Step 2:- Complete code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_Application_Demo
{
class Program
{
public void PrintSwapTwoNumber1()
{
Console.WriteLine("Enter
first number");
int Num1
= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter
second number");
int Num2
= Convert.ToInt32(Console.ReadLine());
int temp;
Console.WriteLine("Before
Swap\n");
Console.WriteLine("Num1
Values: {0}", +Num1);
Console.WriteLine("Num2
Values: {0}", +Num2);
Console.WriteLine("");
temp
= Num1;
Num1
= Num2;
Num2
= temp;
Console.WriteLine("After
Swap\n");
Console.WriteLine("Num1
Values: {0}", +Num1);
Console.WriteLine("Num2
Values: {0}", +Num2);
}
public void PrintSwapTwoNumber2()
{
Console.WriteLine("Enter
first number");
int Num1
= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter
second number");
int Num2
= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Before
Swap\n");
Console.WriteLine("Num1
Values: {0}", +Num1);
Console.WriteLine("Num2
Values: {0}", +Num2);
Console.WriteLine("");
Num1
= Num1 + Num2;
Num2
= Num1 - Num2;
Num1
= Num1 - Num2;
Console.WriteLine("After
Swap\n");
Console.WriteLine("Num1
Values: {0}", +Num1);
Console.WriteLine("Num2
Values: {0}", +Num2);
}
static void Main(string[]
args)
{
Program p
= new Program();
p.PrintSwapTwoNumber1();
//Without
using temp variable
p.PrintSwapTwoNumber2();
Console.ReadLine();
}
}
}
Example:-
No comments:
Post a Comment