Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Declare two integer variables and assign them with 5 and 10 and after that exchange their values.*/
- using System;
- namespace _11_IntegerExchange
- {
- class IntegerExchange
- {
- static void Main()
- {
- int a = 5;
- int b = 10;
- int c;
- Console.WriteLine("First number:{0} , Second number:{1}", a, b);
- c = a;
- a = b;
- b = c;
- Console.WriteLine();
- Console.WriteLine("After Exchange: ");
- Console.WriteLine("First number:{0} , Second number:{1}", a, b);
- Console.WriteLine();
- // Or without using 3th variable.
- //The problem with this 2nd method is that it can cause overflow if the variables contain too big integers
- a = 5;
- b = 10;
- Console.WriteLine("First number:{0} , Second number:{1}", a, b);
- a = a + b;
- b = a - b;
- a = a - b;
- Console.WriteLine();
- Console.WriteLine("After Exchange: ");
- Console.WriteLine("First number:{0} , Second number:{1}", a, b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment