Advertisement
Hazem3529

Swap range sum

Mar 21st, 2015
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2. namespace ConsoleApplication1
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {   // the range [x,y] closed
  8.             int x = Convert.ToInt32(Console.ReadLine());
  9.             int y = Convert.ToInt32(Console.ReadLine());
  10.             int sum =0; // will carry the sum :3 declared out side loop scoop with value = 0
  11.             int temp;
  12.             //swap
  13.             if (x > y) {
  14.                 temp = x;
  15.                 x = y;
  16.                 y = temp;
  17.          
  18.             }
  19.  
  20.  
  21.             while (x <= y) { //removed 5 we 7atet y as max range
  22.                //simple sum
  23.                 sum += x;
  24.                 // the increment to make the loop end
  25.                 x++;
  26.             }
  27.             //the answer out side the loop
  28.             Console.WriteLine("the sum is : {0}", sum);
  29.            
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement