VelizarAvramov

05. Special Numbers

Nov 5th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _05._Special_Numbers
  4. {
  5.     class SpecialNumbers
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //A number is special when its sum of digits is 5, 7 or 11.
  10.             //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
  11.             //(True / False).
  12.  
  13.             int n = int.Parse(Console.ReadLine());
  14.             //356
  15.             //356 % 10 -> 6 !
  16.             //356 / 10 -> 35
  17.             //35 % 10 -> 5 !
  18.             //35 / 10 -> 3
  19.             //3 % 10 -> 3!
  20.             // 3 / 10 -> 0
  21.  
  22.             for (int i = 1; i <= n; i++)
  23.             {
  24.                 int sum = 0;
  25.                 int current = i;
  26.  
  27.                 while (current > 0)
  28.                 {
  29.                     sum += current % 10;
  30.                     current = current / 10;
  31.                 }
  32.                 bool isSpecial = false;
  33.                 isSpecial = (sum == 5 || sum == 7 || sum == 11);
  34.                 Console.WriteLine($"{i} -> {isSpecial}");              
  35.             }          
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment