Saturday , 11 November 2017
Home » ICSE Important Programs » Program on Decimal to Octal Number Conversion

Program on Decimal to Octal Number Conversion

Question:

Write a Program in Java to input a number in Decimal number system and convert it into its equivalent number in the Octal number system.

Note: Octal Number system is a number system which can represent a number in any other number system in terms of digits ranging from 0 to 7 only. This number system consists of only eight basic digits i.e. 0, 1, 2, 3, 4, 5, 6 and 7.

For Example: 25 in the Decimal number system can be represented as 31 in the Octal number system.

decimal to octal conversion
Figure Illustrating Decimal to Octal Number System Conversion

Solution:

/**
* The class Dec2Oct inputs a Decimal number and converts it into its equivalent Octal number
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Dec2Oct
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
        System.out.print("Enter a decimal number : ");
        int n=Integer.parseInt(br.readLine());

        int r;
        String s=""; //variable for storing the result

        //array storing the digits (as characters) in the octal number system
        char dig[]={'0','1','2','3','4','5','6','7'};

        while(n>0)
            {
                r=n%8; //finding remainder by dividing the number by 8
                s=dig[r]+s; //adding the remainder to the result and reversing at the same time
                n=n/8;
            }
        System.out.println("Output = "+s);
    }
}

Output:

Enter a decimal number : 25
Output = 31

Enter a decimal number : 465
Output = 721

[Note: Don’t forget to share these resources and links from our website in your social networking sites with your friends and followers.]

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

5 comments

  1. Thank you so much!!!

  2. i find this site awesome

  3. how to do the above program using recursion…..???? please reply …URGENT…

Leave a Reply

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