Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace NumberDivisible
- {
- using System;
- using System.Linq;
- class Program
- {
- static void Main(string[] args)
- {
- //int[] num = { 9, 2, 5, 8, 1 };
- Console.WriteLine("enter a binary number: ");
- string input = Console.ReadLine();
- //long decimalValue = ToDecimal("101010");
- long decimalValue = ToDecimal(input);
- Console.WriteLine($"Base 10 representation: {decimalValue}");
- int[] nums = decimalValue.ToString().Select(c => int.Parse(c.ToString())).ToArray();
- int sum = Calc(nums, nums.Length - 1);
- bool isDivisibleBy3 = sum % 15 == 0 || sum % 5 == 0 || sum % 3 == 0;
- Console.WriteLine(isDivisibleBy3);
- Console.ReadLine();
- }
- public static long ToDecimal(string binary)
- {
- // 0001
- long value = 0;
- int j = 0;
- for (int i = binary.Length - 1; i >= 0; i--)
- {
- value += (binary[i] == '1' ? 1 : 0) * (int)Math.Pow(2, j++);
- }
- return value;
- }
- public static int Calc(int[] numChar, int i)
- {
- if (i < 0)
- {
- return 0;
- }
- return numChar[i] + Calc(numChar, i - 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement