将整数值转换为二进制值的Java程序
整数是基数为 10 的数。二进制数是以 2 为基数表示的数。二进制仅由 2 位数字 0 和 1 组成。两个运算符用于将整数转换为二进制模和除法以将给定的输入转换为二进制数。
例子:
Input: = 45
Output: = 101101
Input: = 32
Output: = 100000
有许多方法可以将整数转换为二进制数,这里讨论了其中的一些。我们将讨论其中两个:
- 使用堆栈的实现
- 使用内置方法- t oBinaryString() Java Integer 类的
方法一:使用堆栈的实现
实际上,二进制数仅由 0 和 1 组成。要将整数转换为二进制,将数字除以 2 直到它变为 0。在每一步中取模 2 并将余数存储到数组或堆栈中。如果我们将余数存储到一个数组中,则将其以相反的顺序打印。如果我们将余数存储到堆栈中,那么只需一个一个元素弹出并打印它。
下面是上述方法的Java实现:
Java
// Java Program to Convert Integer Values into Binary
// Importing CLasses/Files
import java.io.*;
public class GFG {
// Function to print binary number
static void printBinary(int[] binary, int id)
{
// Iteration over array
for (int i = id - 1; i >= 0; i--)
System.out.print(binary[i] + "");
}
// Function converting decimal to binary
public static void decimalToBinary(int num)
{
// Creating and assigning binary array size
int[] binary = new int[35];
int id = 0;
// Number should be positive
while (num > 0) {
binary[id++] = num % 2;
num = num / 2;
}
// Print Binary
printBinary(binary, id);
}
// Main Driver Code
public static void main(String[] args)
{
// Entered number to be convert into binary
int num = 45;
// Calling Our Above Function
decimalToBinary(num);
}
}
Java
// Java Program to Convert Integer Values into Binary
// Importing Classes/Files
import java.io.*;
import java.util.Stack;
public class GFG {
// Function to convert integer to binary
static void decimalToBinary(int num)
{
// Creating Stack Object Vector
Stack st = new Stack<>();
// Number Should be positive
while (num > 0) {
// Pushing numbers inside stack that
// are divisible by 2
st.push(num % 2);
// Dividing number by 2
num = num / 2;
}
// Checking condition whether stack is empty
while (!(st.isEmpty())) {
// Printing binary number
System.out.print(st.pop());
}
}
// Main driver function
public static void main(String[] args)
{
// Entered number to be converted into binary
int num = 45;
decimalToBinary(num);
}
}
Java
// Java Program to Convert Integer Values into Binary
// Importing Classes/Files
import java.io.*;
class GFG {
// Function converting decimal to binary
static void decimalToBinary(int num)
{
// Function to print integer to binary using
// inbuilt toBinaryString method
System.out.println(Integer.toBinaryString(num));
}
// Main driver function
public static void main(String[] args)
{
// Number to be converted into binary
int num = 45;
// Calling function
decimalToBinary(num);
}
}
输出
101101
通过创建对象向量使用堆栈
Java
// Java Program to Convert Integer Values into Binary
// Importing Classes/Files
import java.io.*;
import java.util.Stack;
public class GFG {
// Function to convert integer to binary
static void decimalToBinary(int num)
{
// Creating Stack Object Vector
Stack st = new Stack<>();
// Number Should be positive
while (num > 0) {
// Pushing numbers inside stack that
// are divisible by 2
st.push(num % 2);
// Dividing number by 2
num = num / 2;
}
// Checking condition whether stack is empty
while (!(st.isEmpty())) {
// Printing binary number
System.out.print(st.pop());
}
}
// Main driver function
public static void main(String[] args)
{
// Entered number to be converted into binary
int num = 45;
decimalToBinary(num);
}
}
输出
101101
方法 2:使用toBinaryString() Java Integer 类的内置方法
Java
// Java Program to Convert Integer Values into Binary
// Importing Classes/Files
import java.io.*;
class GFG {
// Function converting decimal to binary
static void decimalToBinary(int num)
{
// Function to print integer to binary using
// inbuilt toBinaryString method
System.out.println(Integer.toBinaryString(num));
}
// Main driver function
public static void main(String[] args)
{
// Number to be converted into binary
int num = 45;
// Calling function
decimalToBinary(num);
}
}
输出
101101