Java program to convert decimal to octal vice versa octal to decimal using standard values, recursion, array, and vice versa. Here are the basic steps just to let you know the conversion from decimal to octal in math. If you have a clear idea about the conversion then skip the below tutorial and check out the programming – Here is the complete list of programs for beginners.
- What is octal?
An octal is a computer-based system with base eight. The digits in math are 0, 1, 2, 3, 4, 5, 6, 7. The value to the base eight represents as single 8 or Zero or 108.
- What is Decimal?
A decimal number system is a term with base 10. It’s most commonly used number system. The decimal number system consists of 10 digits from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
How to convert from decimal to octal?
For Example, take one number e.g. 1792 when it is divided by 8, you will get quotient ‘ 224 ‘ remainder ‘ 0 ‘.Followed by 224 /8 you will get Q = 28, R = 0 if you continue like that the result in octal as = 3400. Hope you get it. Here is the Java code to print conversion.
1. Using Static Method
[wp_ad_camp_3]
- Here is the code in static format #with sample outputs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Scanner; class DECToOCT { static int i=1; public static void main(String args[]) { int dec, rem, q,j=1; Scanner sc = new Scanner(System.in); System.out.println("Enter a Dec number: "); dec=sc.nextInt(); System.out.println("Oct number is : "); int[] oct=octal(dec); for(int c=i-1;c>0;c--) { System.out.print(oct[c]); } } static int[] octal(int q) { int a[] = new int[50]; while(q != 0) { a[i++] = q%8; q= q/8; } return a; } } |
Output:
1 2 3 4 |
Enter a Dec number: 52 Oct number is : 64 |
2. Using Recursion Method
Here is the code using recursion method with sample output code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; class DECToOCT { static int oct[] = new int[50],i=1; int[] octal(int q) { if(q != 0) { oct[i++] = q%8; q= q/8; octal(q); } return oct; } public static void main(String args[]) { DECToOCT d=new DECToOCT(); int dec, rem, q; Scanner sc = new Scanner(System.in); System.out.println("Enter a Dec number: "); dec=sc.nextInt(); System.out.println("Oct number is : "); int[] oct=d.octal(dec); for(int c=i-1;c>0;c--) { System.out.print(oct[c]); } } } |
1 2 3 4 |
Enter a Dec number: 256 Oct number is : 400 |
3. Basic Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; class DECToOCT { static int i=1; public static void main(String args[]) { int dec, rem,i=0; Scanner sc = new Scanner(System.in); System.out.println("Enter a Dec number: "); dec=sc.nextInt(); System.out.println("Oct number is : "); int a[] = new int[50]; while(dec!= 0) { a[i++] = dec%8; dec= dec/8; } for(int c=i-1;c>=0;c--) { System.out.print(a[c]); } } } |
1 2 3 4 |
Enter a Dec number: 16 Oct number is : 20 |
4. Convert Octal To Decimal: Vice versa
Here is the code from to convert from octal to decimal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class OctalToDecimal { public static void main(String arg[]) { int oct, dec=0, i=0, t; Scanner sc = new Scanner(System.in); System.out.println("Enter Oct Number : "); oct = sc.nextInt(); while(oct != 0) { dec =dec + (oct%10) *(int)Math.pow(8, i); i++; oct = oct/10; } System.out.println("Deci Number is : "+dec); } } |
Output:
1 2 3 |
Enter Oct Number : 65 Deci Number is : 53 |