Convert a Decimal Number to Binary Using Division in C#

In this exercise, we will create a C# program that converts a decimal number into its binary representation using successive divisions by 2. The program will repeatedly ask for a decimal number until the user types "end." Unlike standard methods, this exercise will not use `ToString()` for conversion. Instead, it will implement the binary conversion manually, reinforcing an understanding of loops, conditionals, and number systems.



Group

C# Basic Data Types Overview

Objective

1. Continuously prompt the user to enter a decimal number.
2. Convert the decimal number to binary using successive divisions by 2.
3. Display the binary equivalent as a string of `0`s and `1`s.
4. Repeat the process until the user enters "end."
5. Ensure proper input handling to avoid errors when entering non-numeric values.

Write a C# program that asks the user for a decimal number and displays its equivalent in binary form. It should be repeated until the user enters the word "end." You must not use "ToString", but successive divisions.

Example C# Exercise

 Copy C# Code
using System;

class Program
{
    static void Main()
    {
        string input;
        
        // Infinite loop to keep asking for input until "end" is entered
        while (true)
        {
            Console.Write("Enter a decimal number (or type 'end' to exit): ");
            input = Console.ReadLine();

            // Check if the user wants to exit
            if (input.ToLower() == "end")
            {
                break;
            }

            // Try to parse the input into an integer
            if (int.TryParse(input, out int decimalNumber))
            {
                string binaryResult = "";

                // Convert decimal to binary using successive divisions
                if (decimalNumber == 0)
                {
                    binaryResult = "0";
                }
                else
                {
                    while (decimalNumber > 0)
                    {
                        binaryResult = (decimalNumber % 2) + binaryResult;
                        decimalNumber /= 2;
                    }
                }

                // Display the binary equivalent
                Console.WriteLine($"Binary: {binaryResult}\n");
            }
            else
            {
                Console.WriteLine("Invalid input! Please enter a valid decimal number.\n");
            }
        }

        Console.WriteLine("Program ended.");
    }
}

 Output

Enter a decimal number (or type 'end' to exit): 10
Binary: 1010

Enter a decimal number (or type 'end' to exit): 25
Binary: 11001

Enter a decimal number (or type 'end' to exit): 0
Binary: 0

Enter a decimal number (or type 'end' to exit): end
Program ended.

Share this C# Exercise

More C# Practice Exercises of C# Basic Data Types Overview

Explore our set of C# Practice Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.