Advertisement
VKNikov

Exchanging integer numbers of a and b if a > b

Mar 22nd, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. //Problem 1.    Exchange If Greater
  2. //Write an if-statement that takes two integer variables a and b and exchanges their values if the first one is greater than the second one. As a result print the values a and b, separated by a space.
  3.  
  4. using System;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.     //Note: I made it work only for integers, just like in the assignment. Any other input, doubles or strings will be displayed but
  11.     //not exchanged.
  12.         Console.WriteLine("This program takes two integers a and b and exchanges their values.");
  13.         Console.WriteLine("Please enter value for integer a:");
  14.         string a = Console.ReadLine();
  15.         Console.WriteLine("Please enter value for integer b:");
  16.         string b = Console.ReadLine();
  17.         int c;
  18.         int d;
  19.  
  20.         bool aisint = int.TryParse(a, out c);
  21.         bool bisint = int.TryParse(b, out d);
  22.  
  23.         if (aisint == false || bisint == false || c < d)
  24.         {
  25.             Console.WriteLine("{0} {1}", a, b);
  26.         }
  27.         else if (c > d)
  28.         {
  29.             int e;
  30.             e = c;
  31.             c = d;
  32.             d = e;
  33.             Console.WriteLine("{0} {1}", c, d);
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement