给定一个十进制数作为输入,我们需要编写一个程序将给定的十进制数转换为等效的二进制数。
例子 :
Input : 7
Output :111
Input :10
Output :1010
我们在下面的文章中讨论了一种迭代解决方案。
十进制转换为二进制的程序
以下是递归解决方案:
findBinary(decimal)
if (decimal == 0)
binary = 0
else
binary = decimal % 2 + 10 * (findBinary(decimal / 2)
分步执行的过程,可以更好地了解算法的工作原理
设十进制数为10。
步骤1-> 10%2等于-0 + 10 *(10/2)%2
步骤2-> 5%2等于1 + 10 *(5/2)%2
步骤3-> 2%2等于0 + 10 *(2/2)%2
步骤4-> 1%2等于1 + 10 *(1/2)%2
C++
// C++ program for decimal to binary
// conversion using recursion
#include
using namespace std;
// Decimal to binary conversion
// using recursion
int find(int decimal_number)
{
if (decimal_number == 0)
return 0;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
// Driver code
int main()
{
int decimal_number = 10;
cout << find(decimal_number);
return 0;
}
// This code is contributed by shivanisinghss2110
C
// C/C++ program for decimal to binary
// conversion using recursion
#include
// Decimal to binary conversion
// using recursion
int find(int decimal_number)
{
if (decimal_number == 0)
return 0;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
// Driver code
int main()
{
int decimal_number = 10;
printf("%d", find(decimal_number));
return 0;
}
Java
// Java program for decimal to binary
// conversion using recursion
import java.io.*;
class GFG
{
// Decimal to binary conversion
// using recursion
static int find(int decimal_number)
{
if (decimal_number == 0)
return 0;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
// Driver Code
public static void main(String args[])
{
int decimal_number = 10;
System.out.println(find(decimal_number));
}
}
// This code is contributed by Nikita Tiwari
Python3
# Python3 code for decimal to binary
# conversion using recursion
# Decimal to binary conversion
# using recursion
def find( decimal_number ):
if decimal_number == 0:
return 0
else:
return (decimal_number % 2 + 10 *
find(int(decimal_number // 2)))
# Driver Code
decimal_number = 10
print(find(decimal_number))
# This code is contributed
# by "Sharad_Bhardwaj"
C#
// C# program for decimal to binary
// conversion using recursion
using System;
class GFG
{
// Decimal to binary conversion
// using recursion
static int find(int decimal_number)
{
if (decimal_number == 0)
return 0;
else
return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}
// Driver Code
public static void Main()
{
int decimal_number = 10;
Console.WriteLine(find(decimal_number));
}
}
// This code is contributed by vt_m
PHP
C++
// C++ program for decimal to binary
// conversion using recursion
#include
using namespace std;
// Function to convert decimal to binary
void deci_to_bin(int x, string & bin_num)
{
// Base Case
if (x <= 1)
bin_num += (char)(x + '0');
else {
// Recursion call
deci_to_bin(x / 2, bin_num);
// If x is divisible by 2
if(x%2)
bin_num += '1';
// otherwise
else
bin_num += '0';
}
}
// Driver Code
int main()
{
string bin_num = "";
deci_to_bin(1048576, bin_num);
cout<
Python3
# Python3 code for decimal to binary
# conversion using recursion
# Decimal to binary conversion
# using recursion
def getbinary(number):
# Base case
if number == 0:
return 0
# Recursion call and storing the result
smallans = getbinary(number // 2)
return number % 2 + 10 * smallans
# Driver Code
decimal_number = 1048576
print(getbinary(decimal_number))
# This code is contributed
# by "Sarthak Sethi"
输出
1010
除非您希望将大于1023的十进制数字转换为二进制,否则上述方法可以正常工作。 1024的二进制数是10000000000(1和10的0),超出了int的范围。即使使用long long unsigned作为返回类型,您可以得到的最大值是1048575,这远小于int的范围。一种更简单但有效的方法是将二进制数的各个数字存储在bool向量中。
C++
// C++ program for decimal to binary
// conversion using recursion
#include
using namespace std;
// Function to convert decimal to binary
void deci_to_bin(int x, string & bin_num)
{
// Base Case
if (x <= 1)
bin_num += (char)(x + '0');
else {
// Recursion call
deci_to_bin(x / 2, bin_num);
// If x is divisible by 2
if(x%2)
bin_num += '1';
// otherwise
else
bin_num += '0';
}
}
// Driver Code
int main()
{
string bin_num = "";
deci_to_bin(1048576, bin_num);
cout<
Python3
# Python3 code for decimal to binary
# conversion using recursion
# Decimal to binary conversion
# using recursion
def getbinary(number):
# Base case
if number == 0:
return 0
# Recursion call and storing the result
smallans = getbinary(number // 2)
return number % 2 + 10 * smallans
# Driver Code
decimal_number = 1048576
print(getbinary(decimal_number))
# This code is contributed
# by "Sarthak Sethi"
输出
100000000000000000000