C sharp please Create a Windows app that converts decimal numbers to binary numb
ID: 3702730 • Letter: C
Question
C sharp please
Create a Windows app that converts decimal numbers to binary numbers. Your project will need two text boxes, one to display the decimal number and one to display the binary number. You need a Convert button to kick off the conversion and a Clear button to clear the text boxes. Your program should use a stack to store the converted number. Here is the algorithm for converting a decimal number to a binary number:
n = decimal number
B = base (2 in this case)
1. Take n % B and push result to the stack.
2. n = n / B
3. Repeat 1 and 2 until n < 1
4. Continually pop stack into a string until stack is empty. The resulting string is the converted number.
Explanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DEC_TO_BIN
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Button Control to convert Decimal to Binary
private void button1_Click(object sender, EventArgs e)
{
int DecimalNumber = int.Parse(Decimal_txtbox.Text);//Read Decimal number from the textbox
int quotient,i;
string stack="";
//Below loop push result to the stack.
While(DecimalNumber>=1)
{
quotient = DecimalNumber / 2;
stack = stack + (DecimalNumber % 2).ToString();
DecimalNumber = quotient;
}
int stackLength=stack.Length-1;
while (stackLength < 7)
{
stack = stack + "0";
stackLength++;
}
string BinaryNumber = "";
//Continually pop stack into a string until stack is empty
for (i = stackLength; i >= 0; i--)
{
BinaryNumber = BinaryNumber + stack[i];
}
Binary_txtbox.Text = BinaryNumber.ToString();//Assign Binary number to Textbox for Display
}
//Button Control to clear Textbox
private void button2_Click(object sender, EventArgs e)
{
Decimal_txtbox.Text = "";
Binary_txtbox.Text = "";
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.