Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
Binary Representation of Previous Number in JavaScript
Next article icon

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

Last Updated : 18 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer number, now we convert the given integer number into a binary number using recursion. Recursion is a method in which a function calls itself directly or indirectly and such type of function is known as a recursive function. It solves the problem very efficiently like we find the binary equivalent of an integer.

Examples:

Input : 10  Output: 1010    Input : 11  Output: 1011

Approach: 

To display the binary equivalent of an integer we use the following steps:

  • If condition is used to check if the given value is not equal to zero.
  • If the given condition is true then perform the modulus of the val by 2, then add the modulus result to 10 and then multiply the value of the result with the value of decimaltobinary() function.
  • Now repeat step 2 until the value of val variable is greater than zero.
  • Print the array in reverse order now.
  • And if the condition is false then it will execute the else section, i.e., return 0

The below image can help you better understand the approach.

Let us considered the integer number is 10. Now we find the binary equivalent of 10 so,

  • 10 % 2 + 10 * (10 / 2) % 2 will return 0
  • 5 % 2 + 10 * (5 / 2) % 2 will return 1
  • 2 % 2 + 10 * (2 / 2) % 2 will return 0
  • 1 % 2 + 10 * (1 / 2) % 2 will return 1

So the final result is 1010.

Example 1:

C#
// C# program to display the binary equivalent // of an integer using System;  class GFG{  // Driver code public static void Main(string[] args) {          // Input     int num = 15;          decimaltobinary(num); }  // Function to display the binary equivalent  // of an integer public static int decimaltobinary(int val) {     int binary;          if (val != 0)     {         binary = (val % 2) + 10 * decimaltobinary(val / 2);         Console.Write(binary);         return 0;     }     else     {         return 0;     } } } 

Output
1111

Example 2:

C#
// C# program to display the binary equivalent // of an integer using System;  class GFG{    // Function to display the binary equivalent  // of an integer public static int decimaltobinary(int val) {     int binary;          if (val != 0)     {         binary = (val % 2) + 10 * decimaltobinary(val / 2);         Console.Write(binary);         return 0;     }     else     {         return 0;     } }    // Driver code public static void Main(string[] args) {     int num;          // Reading input from user     Console.Write("Hi! Enter the number:");     num = int.Parse(Console.ReadLine());     decimaltobinary(num);  } } 

Output:

Hi! Enter the number:10  1010

Next Article
Binary Representation of Previous Number in JavaScript

P

pulamolusaimohan
Improve
Article Tags :
  • C#
  • CSharp LINQ
  • CSharp-programs

Similar Reads

  • Find the Number Using Bitwise Questions I
    Given a task to find a number n. There is a pre-defined API int commonSetBits(int val) that returns the number of bits where both n and val have a value of 1 in the corresponding position of their binary representation. In other words, it returns the number of set bits in the bitwise AND (&) ope
    4 min read
  • Binary Representation of Previous Number in JavaScript
    Given a binary input of a positive number, our task is to find the binary representation of a number that is one less than a given binary number n in JavaScript. Example: Input: 100010Output: 100001Explanation: The decimal of 100010 is 34 and 100001 is 33.ApproachThe function "previousBinary1" conve
    1 min read
  • Printing all subsets of {1,2,3,...n} without using array or loop
    Given a natural number n, print all the subsets of the set [Tex]\{1, 2, 3, ..., n\} [/Tex]without using any array or loop (only the use of recursion is allowed).Examples: Input : n = 4 Output : { 1 2 3 4 } { 1 2 3 } { 1 2 4 } { 1 2 } { 1 3 4 } { 1 3 } { 1 4 } { 1 } { 2 3 4 } { 2 3 } { 2 4 } { 2 } {
    8 min read
  • Binary to Decimal Converter
    Binary to Decimal Converter is a free online tool to convert binary to decimal. Converting between binary to decimal is a common task in everyday life. Here, GeeksforGeeks provides a free user-friendly, and efficient online binary decimal Conversion tool to simplify this process and ensure accuracy.
    9 min read
  • Counting Set bit in C
    In C programming, counting set bits is the process of determining the number of bits that are set to 1 in a binary number. This operation is useful in various applications including network programming, error detection, and cryptography. In this article, we will learn how to count the number of set
    4 min read
  • bsearch() Function in C
    In C programming language, bsearch() function is used to perform a binary search on a sorted array to search for an element. The name bsearch stands for “binary search” which is an efficient algorithm for finding an item from a sorted list of items. It is defined inside the stdlib.h header file. Syn
    3 min read
  • Number System Conversion in C
    Number system conversion is a fundamental concept in computer science and programming. It involves changing the representation of a number from one base to another, such as converting a decimal number to binary or a hexadecimal number to binary. In this article, we will create a console program in t
    8 min read
  • Ternary Search in C
    When searching for a specific element in a sorted dataset, many programmers are familiar with binary search. Binary search efficiently narrows down the search space by dividing it into two halves repeatedly. But there's another search algorithm that can be even more efficient in certain scenarios. T
    4 min read
  • CSES Solutions - Weird Algorithm
    Consider an algorithm that takes as input a positive integer N. If N is even, the algorithm divides it by two, and if N is odd, the algorithm multiplies it by three and adds one. The algorithm repeats this, until N is one. Your task is to simulate the execution of the algorithm for a given value of
    4 min read
  • C program to count zeros and ones in binary representation of a number
    Given a number N, the task is to write C program to count the number of 0s and 1s in the binary representation of N. Examples: Input: N = 5 Output: Count of 0s: 1 Count of 1s: 2 Explanation: Binary representation of 5 is "101".Input: N = 22 Output: Count of 0s: 2 Count of 1s: 3 Explanation: Binary r
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences