Question
Program to convert binary to octal
ENTER BINARY NUMBER:11110
THE OCTAL VALUE OF BINARY 11110 IS 36
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class BinaryToOctal
{
public static void main()
{
int count=0;
Scanner sc=new Scanner(System.in);
long binary=0,octal=0,decimal=0,temp=0;
count=0;
System.out.print("ENTER BINARY NUMBER:");
binary=sc.nextInt();
temp=binary;
while(temp>0)
{
decimal=decimal+(temp%10)*((long)Math.pow(2,count));
count++;
temp=temp/10;
}
temp=decimal;
count=0;
while(temp>0)
{
octal=octal+(temp%8)*(long)Math.pow(10,count);
count++;
temp=temp/8;
}
System.out.println("THE OCTAL VALUE OF BINARY "+binary+" IS "+octal);
}
}