JulianJulianov

10. MethodsExercise-Top Number

Feb 15th, 2020
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. 10. Top Number
  2. A top number is an integer that holds the following properties:
  3. • Its sum of digits is divisible by 8, e.g. 8, 16, 88.
  4. • Holds at least one odd digit, e.g. 232, 707, 87578.
  5. Write a program to print all master numbers in the range [1…n].
  6.  
  7. Examples
  8. Input   Output      Input   Output
  9. 50      17          100     17
  10.         35                  35
  11.                             53
  12.                             71
  13.                             79
  14.                             97
  15.  
  16. using System;
  17.  
  18. namespace _10TopNumber
  19. {
  20.     class Program
  21.     {
  22.         public static void Main(String[] args)
  23.         {
  24.             int num = int.Parse(Console.ReadLine());
  25.  
  26.             for (int i = 1; i <= num; i++)
  27.             {
  28.                 if (devisibleByEigth(i) && oddDigit(i))//Проверка дали при двата метода е върнато true!
  29.                 {
  30.                     Console.WriteLine(i);
  31.                 }
  32.             }
  33.         }
  34.         static bool devisibleByEigth(int num)
  35.         {
  36.             int sum = 0;
  37.             while (num > 0)
  38.             {
  39.                 sum += num % 10;//Сумиране на цифрите от числото!
  40.                 num /= 10;//Отделяне на цифра от числото!
  41.             }
  42.             if (sum % 8 == 0)// Проверка дали сумата от цифрите на числото се дели на 8!
  43.             {
  44.                 return true;
  45.             }
  46.             else
  47.             {
  48.                 return false;
  49.             }
  50.         }
  51.         static bool oddDigit(int num)
  52.         {
  53.             int counter = 0;
  54.             while (num > 0)
  55.             {
  56.                 if ((num % 10) % 2 != 0)//Проверка дали поне една цифра от числото е нечетна!
  57.                 {
  58.                     counter++;
  59.                     break;
  60.                 }
  61.                 num /= 10;
  62.             }
  63.             if (counter == 1)
  64.             {
  65.                 return true;
  66.             }
  67.             else
  68.             {
  69.                 return false;
  70.             }
  71.         }
  72.     }
  73. }
Add Comment
Please, Sign In to add comment