on Leave a Comment

Java Program to Convert Decimal to Octal

This java program converts decimal value to its equivalent octal value. First enter a decimal number and converts this number into its equivalent octal value. And finally display that octal value.

Java Program to Convert Decimal to Octal

import java.util.Scanner;
public class DecToOct
{
 public static void main(String[] args) {
 int decimal;
        Scanner scan = new Scanner(System.in);
  
        System.out.print("Enter a Decimal Number : ");
        decimal = scan.nextInt();
  
 int q, i=1, j;
        q = decimal;
          
 int octal[] = new int[100];
        while(q != 0)
        {
            octal[i++] = q%8;
            q = q/8;
        }
  
        System.out.print("Octal Equivalent of " + decimal + " is : ");
        for(j=i-1; j>0; j--)
        {
            System.out.print(octal[j]);
        }
 }
}

Output:

Enter any Decimal Number : 66
Octal Equivalent of 66 is : 102


.

0 comments:

Post a Comment