Advertisement
desislava_topuzakova

04. Sum of Two Numbers

Jul 9th, 2022
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2.  
  3. namespace P04.SumOfTwoNumbers
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //Start of the interval
  10.             int start = int.Parse(Console.ReadLine());
  11.             //End of the interval
  12.             int end = int.Parse(Console.ReadLine());
  13.             int magicNum = int.Parse(Console.ReadLine());
  14.  
  15.             int combinationCnt = 0;
  16.             bool comboFound = false;
  17.             for (int firstNum = start; firstNum <= end; firstNum++)
  18.             {
  19.                 for (int secondNum = start; secondNum <= end; secondNum++)
  20.                 {
  21.                     //One Combination -> {firstNum} {secondNum}
  22.                     combinationCnt++;
  23.  
  24.                     if (firstNum + secondNum == magicNum)
  25.                     {
  26.                         Console.WriteLine($"Combination N:{combinationCnt} ({firstNum} + {secondNum} = {magicNum})");
  27.                         comboFound = true;
  28.                         break;
  29.                     }
  30.                 }
  31.  
  32.                 if (comboFound)
  33.                 {
  34.                     break;
  35.                 }
  36.             }
  37.  
  38.             if (!comboFound)
  39.             {
  40.                 Console.WriteLine($"{combinationCnt} combinations - neither equals {magicNum}");
  41.             }
  42.         }
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement