Advertisement
Masovski

[C# Basics][Loops-HW] 13. Binary to Decimal Number

Mar 29th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1. using System;
  2.  
  3. class BinaryToDecimal
  4. {
  5.     static void Main()
  6.     {
  7.         string input = Console.ReadLine();
  8.  
  9.         int lastIndex = input.Length - 1;
  10.        
  11.         long dec = 0;
  12.         for (int i = 0; i < input.Length; i++, lastIndex--)
  13.         {
  14.             if (input[i].ToString().Contains("0"))
  15.             {
  16.                
  17.             }
  18.             else if (input[i].ToString().Contains("1"))
  19.             {
  20.                 long binarySum = 1;
  21.                 for (int j = 0; j < lastIndex; j++)
  22.                 {
  23.                     binarySum *= 2;
  24.                 }
  25.                 dec += binarySum;
  26.             }
  27.         }
  28.         Console.WriteLine(dec);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement