Simple program #5 (Swap number using two
variables)
Before swapping a is : 8
Before swapping b is : 6
After swapping a is : 6
After swapping b is : 8
The program is written in C#
programming language and will accept two numbers as input. The logic of the
program is to swap input number using two numbers.
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
namespace patternProblem.Simple_program
{
class Simple5 //swap number using two variables
{
static void Main(string[] args)
{
Console.Write("Enter first number: ");
int
fn = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int
sn = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("-----Output-----");
Console.WriteLine();
Console.WriteLine("Before swapping a is : {0}", fn);
Console.WriteLine("Before swapping b is : {0}", sn);
fn = fn + sn;
sn = fn - sn;
fn = fn - sn;
Console.WriteLine("After swapping a is : {0}", fn);
Console.WriteLine("After swapping b is : {0}", sn);
Console.ReadKey();
}
}
}
Output:
The first input
number here is 5 and second input number is 2. The program will swap numbers.
The output is shown below:
Enter first number: 5
Enter second number: 2
Before swapping a is: 5
Before swapping b is: 2
After swapping a is: 2
After swapping b is: 5
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table Simple program
Keep learning and sharing...
No comments:
Post a Comment