Java program to convert decimal to octal number.



package demo;
import java.util.Scanner;
public class DecimalToOctal 
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int decimalNumber, remainder, quotient;
  int[] octalNumber = new int[100];
  int i = 1;
  System.out.print("Enter any decimal number: ");
  decimalNumber = sc.nextInt();
  quotient = decimalNumber;
  while (quotient != 0) 
  {
   octalNumber[i++] = (quotient % 8);
   quotient = quotient / 8;
  }
  System.out.print("Equivalent octal value of decimal number " + ( decimalNumber) + ": ");
  for (int j = i - 1; j > 0; j--) {
   System.out.print(octalNumber[j]);
  }
 }
}

Output:
Enter any decimal number: 100
Equivalent octal value of decimal number 100: 144
BUILD SUCCESSFUL (total time: 4 seconds)


No comments:

Post a Comment