Run
//Java program to convert binary number to octal number
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
//scanner class object creation
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter a binary number : ");
int binary = sc.nextInt();
//Declaring variable to store decimal number
int decimal = 0;
//Declaring variable to use in power
int n = 0;
//writing logic for the conversion from binary to decimal
while(binary > 0)
{
int temp = binary%10;
decimal += temp*Math.pow(2, n);
binary = binary/10;
n++;
}
int octal[] = new int[20];
int i = 0;
//writing logic for the conversion from decimal to octal
while(decimal > 0)
{
int r = decimal % 8;
octal[i++] = r;
decimal = decimal / 8;
}
//printing result
System.out.print("Octal number : ");
for(int j = i-1 ; j >= 0 ; j--)
System.out.print(octal[j]);
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
String num = sc.nextLine();
try{
int decimal= Integer.parseInt(num,2); // binary to decimal conversion.
String octal=Integer.toOctalString(decimal);
System.out.println(octal);}
catch (Exception e){
System.out.println(“Invalid binary input. Please enter a valid binary number.”);
}
You can join this Channel if you have any technical doubts.
import java.util.*;
public class Binarytooctal {
public static long binarytodecimal(int binary){
long decimal =0;
int x=0;
while(binary >0){
long rem = binary %10;
decimal += rem * Math.pow(2,x);
binary /=10;
}
return decimal;
}
public static List decimaltooctal(long decimal){
List a = new ArrayList();
long octal =0;
while(decimal >0){
long rem = decimal % 8;
a.add(rem);
decimal /=10;
}
return a;
}
public static void main(String[] args){
System.out.print(decimaltooctal(binarytodecimal(111)));
}
}
Kindly join our Discord channel for any technical query : Discord
import java.util.*;
public class Binary_Octal
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a Binary number : “);
int num=Integer.parseInt(sc.nextLine(),2);
String octal=Integer.toOctalString(num);
System.out.println(“Octal version is : “+octal);
}
}
import java.util.*;
class main{
static String octal(String binary){
int temp = 0;
StringBuilder octal = new StringBuilder();
int j = 0;
for(int i=binary.length()-1;i>=0;i–){
if(j == 3){
octal.append(temp);
System.out.println(j+” “+temp);
temp = 0;
j=0;
}
temp += (binary.charAt(i)-‘0’)*Math.pow(2,j);
j++;
}
octal.append(temp);
return “Binary : “+binary+” == octal : “+octal.reverse().toString();
}
public static void main(String[] args) {
String binary = “1011001”;
System.out.println(octal(binary));
}
}
package com.company;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“enter the binary number”);
int binary=input.nextInt();
int l=0,b=0;
int decimal=0;
int octal=0;
while(binary!=0)
{
b=binary%10;
decimal=decimal+(b*((int)Math.pow(2,l)));
binary=binary/10;
b=0;
l++;
}l=0;
System.out.println(“decimal”+decimal);
while(decimal!=0)
{
b=decimal%8;
// System.out.println(b);
octal=octal+(b*(int)Math.pow(10,l));
// System.out.println(octal);
decimal=decimal/8;
l++;
// System.out.println(decimal);
}
System.out.println(“octal”+octal);
}
}