Wednesday, February 15, 2012

Tutorial: Decimal to Octal Coverter in Java

Hi guys. It's nice to here again. Today, I will show you a Number System converter program where you can  convert a DECIMAL value to its equivalent OCTAL value. 

In designing this Java program, I follow the rules in converting decimal value to octal. Here is the sample output of the program.



In the figure above, the user must input first a DECIMAL (e.g 170)  number to be converted. Then, by pressing the Enter key from the keyboard, then the program automatically get or compute its equivalent OCTAL value (e.g. 252). I made the algorithm of converting the value following or applying the rules in said conversion. In addition to that, the program also asked the user if she/he wants to continue converting. Now by pressing big or small letter (Y/y), then the user will be able to the conversion again, else if the user press any key from the keyboard, then definitely the program will be terminated (see the sample output below).

Here is the sample program source code or algorithm that converts the decimal value to octal:

           do
            {
                quotient = dividend/BASE;
                int remainder = (dividend - (quotient*BASE));
                dividend = quotient;
                octal[x] = remainder;
                ++x;
            }while(dividend!=0);


The code above indicates the rules wherein the final result will coming from the variable remainder, so I used an array to store each of the value of the remainder everytime the program loops. The remainder will be stored sequentially in the array starting from index 0. Now, to get the final result, you must read the from bottom to top, meaning that the last remainder stored in the array should be the first to retrieve or printed. To solve this, I have to access the last index of the array going to the first index which is 0 using a looping statement. And this will be my code:

           for (temp = x-1;temp >= 0;temp--)
            {
                System.out.print(octal[temp]);
            }

 

The Code:

Here is the complete source code of the program. It is FREE. You can copy, paste and RUN now!

/* Autor: Vilchor G. Perdido
 * Project: DecimalToOctalConverter v.1.0
 * Date: February 14, 2012
 */

import java.util.*;

public class DecimalToOctalConverter
{
    public static void main(String[] args)
    {
        Scanner read = new Scanner(System.in);
        int quotient = 0;
        final int BASE = 8;
        String choice = "";
        char sagot = ' ';

        do {
            System.out.println("======================================");
            System.out.println("DECIMAL TO OCTAL CONVERTER PROGRAM");
            System.out.println("======================================");
            System.out.println("Please input DECIMAL number to convert:");
            int whole = read.nextInt();
            int x=0,temp=0;
            int dividend=0;
            int octal[] = new int[10];
            dividend = whole;
            System.out.println("\nThe OCTAL value is:");

            do
            {
                quotient = dividend/BASE;
                int remainder = (dividend - (quotient*BASE));
                dividend = quotient;
                octal[x] = remainder;
                ++x;
            }while(dividend!=0);
           
            for (temp = x-1;temp >= 0;temp--)
            {
                System.out.print(octal[temp]);
            }
           
            System.out.println("\n\nWhat do you want? Convert again?");
            System.out.println("Press Y or y to continue or any key to quit.");
            System.out.print("Confirm choice:");
            choice = read.next();
            sagot = choice.charAt(0);
            System.out.println("");
        } while(sagot=='Y' || sagot=='y');
        System.out.println("Program terminated.....");
    }
}

-------------

Happy coding guys!

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...