0% found this document useful (0 votes)
185 views

Example To Convert The Decimal Number To Binary Number & Binary Number To Decimal Number by C# Program

This document contains code snippets for converting between decimal and binary numbers in C#. The first snippet takes a decimal number as input, converts it to a string of binary digits, then reverses the string and outputs the binary number. The second snippet takes a binary number as input, calculates the place value of each digit to determine the decimal equivalent, and outputs the final decimal number.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views

Example To Convert The Decimal Number To Binary Number & Binary Number To Decimal Number by C# Program

This document contains code snippets for converting between decimal and binary numbers in C#. The first snippet takes a decimal number as input, converts it to a string of binary digits, then reverses the string and outputs the binary number. The second snippet takes a binary number as input, calculates the place value of each digit to determine the decimal equivalent, and outputs the final decimal number.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Q6/ Write a C# Program to Convert the Decimal Number to Binary Number.

Solution: int d,a; d = int.Parse(Console.ReadLine()); string dec = ""; while (d >= 1) { a = d / 2; dec += (d % 2); d = a; } string bin = ""; for (int i = dec.Length - 1; i >= 0; i--) { bin = bin + dec[i]; } Console.Write("The Binary number Console.Read(); is {0}",bin);

Q7/ Write a C# Program to Convert the Binary Number to Decimal Number.


Solution: int DecimalNumber = 0, BinaryNumber, power = 1; BinaryNumber = Convert.ToInt32(Console.ReadLine()); while (BinaryNumber > 0) { DecimalNumber += BinaryNumber % 10 * power; BinaryNumber = BinaryNumber / 10; power = power * 2; } Console.WriteLine("The decimal Number is {0}",DecimalNumber); Console.ReadLine();

You might also like