Advertisement
IvetValcheva

04. Sum of Two Numbers

Mar 16th, 2024
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | None | 0 0
  1.  
  2. int startNum = int.Parse(Console.ReadLine());
  3. int finalNum = int.Parse(Console.ReadLine());
  4.  
  5. int magicNum = int.Parse(Console.ReadLine());
  6. int count = 0;
  7.  
  8. bool isFound = false;
  9.  
  10. for (int x = startNum; x <= finalNum; x++)
  11. {
  12.     for (int y = startNum; y <= finalNum; y++)
  13.     {
  14.         int sum = x + y;
  15.         count++;  
  16.        
  17.         if (sum == magicNum)
  18.         {
  19.             Console.WriteLine($"Combination N:{count} ({x} + {y} = {magicNum})");
  20.             isFound = true;
  21.             break;
  22.         }
  23.     }
  24.  
  25.     if (isFound ) { break; }
  26. }
  27.  
  28. if (!isFound)
  29. {
  30.     Console.WriteLine($"{count} combinations - neither equals {magicNum}");
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement