Skip to main content

Java Program for number conversions

The following code is to convert the number i.e., Decimal Number to either Binary or Octal or Hexadecimal Number according to user's choice.
Code:-

import java.util.*;
public class Convert {
    Scanner scan = new Scanner(System.in);
    int choice, dev;
    String r = "\0";
    long num, res, rem, temp;
    public void input() {
        try {
            System.out.printf("1: Decimal to Binary\n2: Decimal to Octal\n3: Decal to Hexadecimal\nEnter your choice: ");
            choice = scan.nextInt();
            System.out.printf("Enter number: ");
            num = scan.nextLong();
        } catch (NumberFormatException | InputMismatchException e) {
            System.err.println("Error Occur!\n" + e.getMessage());
            System.exit(0);
        }
    }
    public void compute() {
        switch (choice) {
            case 1:
                dev = 2;
                break;
            case 2:
                dev = 8;
                break;
            case 3:
                dev = 16;
                break;
            default:
                System.err.println("Invalid Choice!");
                System.exit(0);
        }
        temp = num;
        char cha;
        while (temp != 0) {
            rem = temp % dev;
            temp /= dev;
            if (rem < 10) {
                r = r.concat(Long.toString(rem));
            }
            if (rem > 9) {
                cha = hexadecimal(rem);
                r = r.concat(Character.toString(cha));
            }
        }
        StringBuffer str = (new StringBuffer(r)).reverse();
        r = str.toString();
        System.out.println("Converted Number: " + r);
    }
    public char hexadecimal(long re) {
        char cha;
        if (re == 10) {
            cha = 'A';
        } else if (re == 11) {
            cha = 'B';
        } else if (re == 12) {
            cha = 'C';
        } else if (re == 13) {
            cha = 'D';
        } else if (re == 14) {
            cha = 'E';
        } else {
            cha = 'F';
        }
        return cha;
    }
    public static void main(String[] args) {
        Convert con = new Convert();
        con.input();
        con.compute();
    }
}

Output:-
1: Decimal to Binary
2: Decimal to Octal
3: Decal to Hexadecimal
Enter your choice: 3
Enter number: 50
Converted Number: 32

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...