Decimal To Octal Conversion Java Program

By | 2018-03-21T21:04:28+00:00 June 18th, 2016|java programs|0 Comments

Here we are going to write a program on conversion of Decimal To Octal Numbers in Java.

Decimal To Octal Conversion :


import java.util.Scanner;

public class ConvertDecimalToOctal {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any Decimal Number : ");
        int input = sc.nextInt();
        System.out.println("Oct is : "+convertDecimalToOctal(input));
    }
    
    public static String convertDecimalToOctal(int decimal) {
        String octal = "";
        try {
            if (decimal != 0) {
                while (decimal > 0) {
                    octal = decimal % 8 + octal;
                    decimal /= 8;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return octal;
    }

}

Output :

Enter any Decimal Number :  156
Oct is : 234

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment