This code is an implementation of decimal to octal conversion using recursion. The decimalToOctal method takes an integer dec as input and recursively converts it to its octal representation. The result is stored in a static variable oct which is initialized to 0 at the beginning of the program.
The method works by first checking if dec is not equal to 0. If it is not, it performs the following steps:
Finally, when dec becomes 0, the method returns the value of oct.
import java.util.*; public class DecToOct { static int tmp = 1; static int oct = 0; public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.printf("Enter the Decimal Number : "); int dec = input.nextInt(); int octNum = decimalToOctal(dec); System.out.printf("Octal Number : " + octNum); } public static int decimalToOctal(int dec) { if (dec != 0) { oct = oct + (dec % 8) * tmp; tmp = tmp * 10; decimalToOctal(dec / 8); } return oct; } }
Enter the Decimal Number : 18 Octal Number : 22
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions