kotlin 二进制_Kotlin程序将十进制转换为二进制

news/2024/7/20 2:36:52 标签: python, java, tensorflow, 小程序, c++

kotlin 二进制

We are given a decimal number and we will create a Kotlin program to convert decimal number to binary.

我们给了十进制数,我们将创建一个Kotlin程序将十进制数转换为二进制

To convert decimal number to binary, we will recursively divide the decimal number by 2 and save the remainder found till the decimal number vanquishes.

要将十进制数转换为二进制,我们将十进制数递归除以2,然后将找到的余数保存到十进制数消失。

Example:

例:

    Input:
    Decimal number = 12

    Recursive division: 
    
NumberQuotientRemainder
1260
630
311
101
Output: Here, we will read the remainders in reverse order which is the binary equivalent i.e.
12 6 0
6 3 0
3 1个 1个
1个 0 1个
输出:在这里,我们将以相反的顺序读取余数,即二进制等效项,即 1100 is the binary conversion for 110012. 12的二进制转换。

在Kotlin中将十进制转换为二进制的程序 (Program to convert decimal to binary in Kotlin)

package com.includehelp.basic

import java.util.*

/* function to convert given decimal number into Binary */
fun getBinaryNumber(decimalNumber: Int): String {
    var decimalNumber = decimalNumber
    val binaryStr = StringBuilder()
    
    while (decimalNumber > 0) {
        val r = decimalNumber % 2
        decimalNumber /= 2
        binaryStr.append(r)
    }
    
    return binaryStr.reverse().toString()
}

// Main Method Entry Point of Program
fun main(arg: Array<String>) {
    val sc = Scanner(System.`in`)
    
    println("Enter Decimal Number  : ")
    //Input Decimal Number
    val decimalNumber: Int = sc.nextInt()
    
    // Call function to Convert Decimal into binary
    val binaryNumber = getBinaryNumber(decimalNumber)
    // Print Binary Number
    println("Binary Number : $binaryNumber")
}

Output

输出量

Run 1:
Enter Decimal Number  :
15
Binary Number : 1111
-----
Run 2:
Enter Decimal Number  :
12345
Binary Number : 11000000111001
----
Run 3:
Enter Decimal Number  :
545654
Binary Number : 10000101001101110110


翻译自: https://www.includehelp.com/kotlin/convert-decimal-to-binary.aspx

kotlin 二进制


http://www.niftyadmin.cn/n/1254150.html

相关文章

矩阵逆| 使用Python的线性代数

Prerequisites: 先决条件&#xff1a; Defining a Matrix 定义矩阵 Syntax: 句法&#xff1a; inv_M numpy.linalg.inv(M)Here, "M" is the matrix. 在此&#xff0c;“ M”是矩阵。 Python代码查找矩阵的逆 (Python code to find the inverse of a matrix) # Li…

java 简单的调用类_利用java反射机制实现自动调用类的简单方法

1. 新建TestServlet类package com.yanek.test;import java.io.IOException;import java.lang.reflect.Method;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSer…

内存映射文件及其实现技术_内存映射及其类型

内存映射文件及其实现技术内存映射 (Memory Mapping) The translation between the logical address space and the physical memory is known as Memory Mapping. To translate from logical to a physical address, to aid in memory protection also to enable better manag…

打印机最大黑标长度_打印成对的最大长度链

打印机最大黑标长度Problem statement: 问题陈述&#xff1a; An N pair array is given to you. In every pair, the first number is always smaller than the second number. A pair (a,b) can follow another pair (c,d) if a is greater than d. Two pairs are joined wi…

图书管理系统java课设_JavaGUI图书管理系统(可作课程设计)

图书管理系统刚开始学Java本身写的一个小项目分享一下&#xff1a;mysql源码连接&#xff1a;git功能&#xff1a;系统分管理员界面与用户界面数据库管理员&#xff1a;实现对图书的增、删、改、查&#xff0c;对全部借阅历史的搜索及全部帐户的信息&#xff0c;用户的权限与管…

rint函数 java_rint()函数与C ++中的示例

rint函数 javaC rint()函数 (C rint() function) rint() function is a library function of cmath header. It is used to round the given value to an integral value based on the specified direction by fegetround() function. It accepts a parameter and returns the…

Java边缘填充_边缘填充算法

#include#include#include#include#include#include#define N 100000#define Min -99999999int n;//点的个数int right;//图像的右边界struct point //点的坐标{int x;int y;}point[N];void init() //顺序输入点的坐标{int i;printf("请输入点的个数: ");scanf(&qu…

logb()函数以及C ++中的示例

C logb()函数 (C logb() function) logb() function is a library function of cmath header, it is used to get the logarithm of |given value|, where logarithm base is FLT_RADIX (On the most of the platforms the FLT_RADIX is 2, thus, logb() function is similar …