Write a Java program to convert a binary number to a Octal numberInput Data:Input a Binary Number: 111Expected Output:
Octal number: 7
import java.util.*; public class Exercise24 { public static void main(String[] args) { int binnum, binnum1,rem, decnum=0, quot, i=1, j; int octnum[] = new int[100]; Scanner scan = new Scanner(System.in); System.out.print("Input a Binary Number : "); binnum = scan.nextInt(); binnum1=binnum; while(binnum > 0) { rem = binnum % 10; decnum = decnum + rem*i; //System.out.println(rem); i = i*2; binnum = binnum/10; } i=1; quot = decnum; while(quot > 0) { octnum[i++] = quot % 8; quot = quot / 8; } System.out.print("Equivalent Octal Value of " +binnum1+ " is :"); for(j=i-1; j>0; j--) { System.out.print(octnum[j]); } System.out.print("\n"); } }
Sample Output:
Enter Binary Number : 111 Equivalent Octal Value of 111 is :7
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer