From ac092bb68bd0b8907d9f963659cce1b73fa564d1 Mon Sep 17 00:00:00 2001 From: Rebin <4rebin@gmail.com> Date: Sat, 23 Oct 2021 16:08:25 +0300 Subject: [PATCH] Add DecimalToBinary and BinaryToDecimal in C# --- algorithms/math/BinaryToDecimal.cs | 30 ++++++++++++++++++++++++++ algorithms/math/DecimalToBinary.cs | 34 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 algorithms/math/BinaryToDecimal.cs create mode 100644 algorithms/math/DecimalToBinary.cs diff --git a/algorithms/math/BinaryToDecimal.cs b/algorithms/math/BinaryToDecimal.cs new file mode 100644 index 0000000..4971606 --- /dev/null +++ b/algorithms/math/BinaryToDecimal.cs @@ -0,0 +1,30 @@ +using System; + +namespace BinaryToDecimal +{ + class Program + { + static void Main(string[] args) + { + BinaryToDecimal(); + Console.ReadKey(); + } + + private static void BinaryToDecimal() + { + Console.Write("Please Enter the Binary Number: "); + int ThebinaryNumber = int.Parse(Console.ReadLine()); + int DecimalValueNumber = 0; + int Base1Number = 1; + + while (ThebinaryNumber > 0) + { + int ReminderNumber = ThebinaryNumber % 10; + ThebinaryNumber = ThebinaryNumber / 10; + DecimalValueNumber += ReminderNumber * Base1Number; + Base1Number = Base1Number * 2; + } + Console.Write("Result of Decimal Value : " + DecimalValueNumber); + } + } +} diff --git a/algorithms/math/DecimalToBinary.cs b/algorithms/math/DecimalToBinary.cs new file mode 100644 index 0000000..4d1fa45 --- /dev/null +++ b/algorithms/math/DecimalToBinary.cs @@ -0,0 +1,34 @@ +using System; + +namespace DecimalToBinary +{ + class Program + { + static void Main(string[] args) + { + + DecimalToBinary(); + + Console.ReadKey(); + } + + private static void DecimalToBinary() + { + Console.Write("Please Enter a Decimal Number: "); + + int inputDecimalNumber = Convert.ToInt32(Console.ReadLine()); + + string resultOfBinary; + + resultOfBinary = string.Empty; + while (inputDecimalNumber > 1) + { + int remainder = inputDecimalNumber % 2; + resultOfBinary = Convert.ToString(remainder) + resultOfBinary; + inputDecimalNumber /= 2; + } + resultOfBinary = Convert.ToString(inputDecimalNumber) + resultOfBinary; + Console.WriteLine("Result In Binary Number: " + resultOfBinary); + } + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy