Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 10. Top Number
- A top number is an integer that holds the following properties:
- • Its sum of digits is divisible by 8, e.g. 8, 16, 88.
- • Holds at least one odd digit, e.g. 232, 707, 87578.
- Write a program to print all master numbers in the range [1…n].
- Examples
- Input Output Input Output
- 50 17 100 17
- 35 35
- 53
- 71
- 79
- 97
- using System;
- namespace _10TopNumber
- {
- class Program
- {
- public static void Main(String[] args)
- {
- int num = int.Parse(Console.ReadLine());
- for (int i = 1; i <= num; i++)
- {
- if (devisibleByEigth(i) && oddDigit(i))//Проверка дали при двата метода е върнато true!
- {
- Console.WriteLine(i);
- }
- }
- }
- static bool devisibleByEigth(int num)
- {
- int sum = 0;
- while (num > 0)
- {
- sum += num % 10;//Сумиране на цифрите от числото!
- num /= 10;//Отделяне на цифра от числото!
- }
- if (sum % 8 == 0)// Проверка дали сумата от цифрите на числото се дели на 8!
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- static bool oddDigit(int num)
- {
- int counter = 0;
- while (num > 0)
- {
- if ((num % 10) % 2 != 0)//Проверка дали поне една цифра от числото е нечетна!
- {
- counter++;
- break;
- }
- num /= 10;
- }
- if (counter == 1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment