Monday , October 23 2017
Home / java programs / Decimal To Octal Conversion Java Program

Decimal To Octal Conversion Java Program

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

About chandrashekhar

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+ ,

Recommended

Java program to find sum of digits

In this Java programming tutorials, I am going to show you how to calculate sum …

Leave a Reply

Your email address will not be published. Required fields are marked *