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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.