Venciity

[ Telerik C#] Primitives And Variables - EX 11

Feb 13th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. /*Declare  two integer variables and assign them with 5 and 10 and after that exchange their values.*/
  2. using System;
  3.  
  4. namespace _11_IntegerExchange
  5. {
  6.     class IntegerExchange
  7.     {
  8.         static void Main()
  9.         {
  10.             int a = 5;
  11.             int b = 10;
  12.             int c;
  13.  
  14.             Console.WriteLine("First number:{0} , Second number:{1}", a, b);
  15.             c = a;
  16.             a = b;
  17.             b = c;
  18.             Console.WriteLine();
  19.             Console.WriteLine("After Exchange: ");
  20.             Console.WriteLine("First number:{0} , Second number:{1}", a, b);
  21.  
  22.             Console.WriteLine();
  23.  
  24.             // Or without using 3th variable.
  25.             //The problem with this 2nd method is that it can cause overflow if the variables contain too big integers
  26.             a = 5;
  27.             b = 10;
  28.             Console.WriteLine("First number:{0} , Second number:{1}", a, b);
  29.  
  30.             a = a + b;
  31.             b = a - b;
  32.             a = a - b;
  33.             Console.WriteLine();
  34.             Console.WriteLine("After Exchange: ");
  35.             Console.WriteLine("First number:{0} , Second number:{1}", a, b);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment