fork download
  1. using System;
  2.  
  3. namespace ConsoleApp4
  4. {
  5. internal class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Console.Write("Введіть n: ");
  10. int n = int.Parse(Console.ReadLine());
  11.  
  12. PrintAllFracs(n);
  13. }
  14.  
  15. // 1. Функція для пошуку найбільшого спільного дільника (НСД)
  16. static int CalcGreatestCommonDivisor(int a, int b)
  17. {
  18. while (b != 0)
  19. {
  20. int temp = b;
  21. b = a % b;
  22. a = temp;
  23. }
  24. return a;
  25. }
  26.  
  27. // 2. Функція для перевірки, чи є дріб нескоротним
  28. static bool IsFracIrreducible(int a, int b)
  29. {
  30. if (CalcGreatestCommonDivisor(a, b) == 1)
  31. {
  32. return true;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39.  
  40. // 3. Функція для виведення всіх нескоротних дробів
  41. static void PrintAllFracs(int n)
  42. {
  43. for (int b = 1; b <= n; b++)
  44. {
  45. for (int a = 0; a <= b; a++)
  46. {
  47. if (IsFracIrreducible(a, b))
  48. {
  49. Console.WriteLine(a + "/" + b);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
Success #stdin #stdout 0.02s 11340KB
stdin
4
stdout
Введіть n: 0/1
1/1
1/2
1/3
2/3
1/4
3/4