Binary to Octal Program in Java

Google Advertisements

Prev Tutorial Next Tutorial

Convert Binary to Octal Program in Java

There are two way to convert decimal number into octal number, which is given below;

  • Using predefined method Integer.toOctalString(int num)
  • Writing our own logic for conversion

Convert Binary to Octal in Java

import java.util.Scanner;
 
public class BinaryTOoctal
{
Scanner scan;
int num;
	
void getVal() 
{	
System.out.println("Binary to Octal");
scan = new Scanner(System.in);
	
System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine(), 2);
}	
void convert() 
{
String octal = Integer.toOctalString(num);
System.out.println("Octal Value is : " + octal);
}
}
 
class MainClass 
{
	
public static void main(String args[]) 
{
Binary_Octal obj = new Binary_Octal();
obj.getVal();
obj.convert();
}
}

Output

Binary to Octal

Enter the number :
1010
Octal Value is : 12

Convert Binary to Octal in Java

import java.util.Scanner;

public class BinaryTOoctal
{
 public static void main(String args[])
 {
 int binnum, rem, quot, i=1, j;
 int octnum[] = new int[100];
 Scanner scan = new Scanner(System.in);
		
 System.out.print("Enter Binary Number : ");
 binnum = scan.nextInt();
		
 quot = binnum;
		
 while(quot != 0)
 {
 octnum[i++] = quot%8;
 quot = quot/8;
 }
		
 System.out.print("Equivalent Octal Value of " +binnum+ " is :\n");
 for(j=i-1; j>0; j--)
 {
 System.out.print(octnum[j]);
 }
}
}

Output

Enter Binary Number : 11
Equivalent Octal Value of 11 is :13

Syntax to compile and run java program

Syntax

for compile -> c:/>javac BinaryTOoctal.java
for run -> c:/>java BinaryTOoctal

Prev Tutorial Next Tutorial

Google Advertisements

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs