Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace _05._Special_Numbers
- {
- class SpecialNumbers
- {
- static void Main(string[] args)
- {
- //A number is special when its sum of digits is 5, 7 or 11.
- //Write a program to read an integer n and for all numbers in the range 1…n to print the number and if it is special or not
- //(True / False).
- int n = int.Parse(Console.ReadLine());
- //356
- //356 % 10 -> 6 !
- //356 / 10 -> 35
- //35 % 10 -> 5 !
- //35 / 10 -> 3
- //3 % 10 -> 3!
- // 3 / 10 -> 0
- for (int i = 1; i <= n; i++)
- {
- int sum = 0;
- int current = i;
- while (current > 0)
- {
- sum += current % 10;
- current = current / 10;
- }
- bool isSpecial = false;
- isSpecial = (sum == 5 || sum == 7 || sum == 11);
- Console.WriteLine($"{i} -> {isSpecial}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment