package Conversions;import java.util.Scanner;/*** Converts any Binary number to an Octal Number** @author Zachary Jones*/public class BinaryToOctal {/*** Main method** @param args Command line arguments*/public static void main(String args[]) {Scanner sc = new Scanner(System.in);System.out.println("Input the binary number: ");int b = sc.nextInt();System.out.println("Octal equivalent: " + convertBinaryToOctal(b));sc.close();}/*** This method converts a binary number to an octal number.** @param binary The binary number* @return The octal number*/public static String convertBinaryToOctal(int binary) {String octal = "";int currBit = 0, j = 1;while (binary != 0) {int code3 = 0;for (int i = 0; i < 3; i++) {currBit = binary % 10;binary = binary / 10;code3 += currBit * j;j *= 2;}octal = code3 + octal;j = 1;}return octal;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。