Java Program to convert Octal Number to Decimal Number and Vice Versa


 

// Program by Akash Tripathi (@proakash256) 
 
import java.util.Scanner;

public class Octal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        byte ch;
        int n;
        System.out.println("Enter 1 to convert
                            Octal Number to
                            Decimal Number.");
        System.out.println("Enter 2 to convert
                            Decimal Number to
                            Octal Number.");
        System.out.println("Enter your Choice :\n");
        ch = sc.nextByte();
        System.out.println();
        if (ch == 1) {
            System.out.print("Enter the Octal Number : ");
            n = sc.nextInt();
            System.out.println();
            int temp = ni = 0s = 0a;
            while (temp > 0) {
                a = temp % 10;
                s = s + (a * (intMath.pow(8i));
                temp = temp / 10;
                i = i + 1;
            }
            System.out.println("\nThe Decimal
                                equivalent of
                                Octal Number " 
                                n + " is : " + s);
        } else if (ch == 2) {
            System.out.print("Enter the Decimal Number : ");
            n = sc.nextInt();
            System.out.println();
            int temp = ni = 0s = 0a;
            while (temp > 0) {
                a = temp % 8;
                s = s + (a * (intMath.pow(10i));
                temp = temp / 8;
                i = i + 1;
            }
            System.out.println("The Octal equivalent
                                of Decimal Number " 
                                n + " is : " + s);
        } else
            System.out.println("Invalid Choice.\n");
        sc.close();
    }
}

Comments