Write a Java method getBinaryString(int toCalc) that takes as argument a (strictly) positive integer toCalc, and returns the binary representation of toCalc as a String. For instance, if toCalc = 13, the method should return the String "1101", since 13 = 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0.
时间: 2023-04-07 12:00:32 浏览: 70
可以回答这个问题。下面是 Java 代码:
```
public static String getBinaryString(int toCalc) {
if (toCalc == 0) {
return "0";
}
StringBuilder sb = new StringBuilder();
while (toCalc > 0) {
sb.append(toCalc % 2);
toCalc /= 2;
}
return sb.reverse().toString();
}
```
相关问题
Write a python function proddigits() that inputs a positive integer number and returns the product of digits of that number.
Here's the Python function `proddigits()` that takes a positive integer as input and returns the product of its digits:
```python
def proddigits(num):
# Initialize product to 1
product = 1
# Convert the integer to a string to loop through its digits
num_str = str(num)
# Loop through each digit in the string
for digit in num_str:
# Multiply the digit to the current product
product *= int(digit)
# Return the final product
return product
```
You can call this function with a positive integer as an argument and it will return the product of its digits. Here's an example usage:
```python
num = 12345
product = proddigits(num)
print(product) # Output: 120
```
In this example, the input integer is 12345 and the product of its digits is 1 x 2 x 3 x 4 x 5 = 120.
Write a function, is_prime, that takes a single integer argument and returns True when the argument is a prime number and False otherwise.
Sure! Here's the Python code for the is_prime function:
```
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
```
This function takes a single integer argument `n` and returns `True` when `n` is a prime number and `False` otherwise. The function first checks if `n` is less than or equal to 1, as 1 is not a prime number. Then it loops through all the integers from 2 to the square root of `n`, checking if any of them divide `n` without a remainder. If any such integer is found, `n` is not a prime number and the function returns `False`. Otherwise, `n` is a prime number and the function returns `True`.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)