C# Program to Print Binary Equivalent of an Integer using Recursion
Posted by Superadmin on August 14 2022 14:29:54

C# Program to Print Binary Equivalent of an Integer using Recursion

 

 

This is a C# Program to print binary equivalent of an integer using recursion.

Problem Description

This C# Program Prints Binary Equivalent of an Integer using Recursion.

Problem Solution

Here this C# program, using recursion, finds the binary equivalent of a decimal number entered by the user. Decimal numbers are of base 10 while binary numbers are of base 2.

Program/Source Code

Here is source code of the C# Program to Print Binary Equivalent of an Integer using Recursion. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Print Binary Equivalent of an Integer using Recursion
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
    public static void Main(string[] args)
    {
        int num;
        prog pg = new prog();
        Console.WriteLine("Enter a decimal number: ");
        num = int.Parse(Console.ReadLine());
        Console.WriteLine("The binary equivalent of num is :");
        pg.binaryconversion(num);
        Console.ReadLine();
    }
}
public class prog
{
    public int binaryconversion(int num)
    {
        int bin;
        if (num != 0)
        {
            bin = (num % 2) + 10 * binaryconversion(num / 2);
            Console.Write(bin);
            return 0;
        }
        else
        {
            return 0;
        }
 
    }
}
Program Explanation

In this C# program, we are reading a decimal number using ‘num’ variable. Decimal numbers are of base 10 while binary numbers are of base 2.

 

Using binary_conversion() function convert the binary number to its equivalent decimal value. If else condition statement is used to check the value of ‘num’ variable is equal to 0. If the condition is true then execute the statement and return the value of 0.

 

 

Otherwise, if the condition is false then execute the else statement. Compute the modulus of the value of ‘num’ variable by 2. Add the value of ‘result’ variable to 10. Multiply the value of ‘result’ variable with the value of binary_conversion() function. Print the binary equivalent of an integer using recursion.

Runtime Test Cases
 
Enter a decimal number:
19
The binary equivalent of num is :
10011