A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Java program to convert number from Decimal to Octal
Q:

Java program to convert number from Decimal to Octal

0

This program will convert integer (Decimal) number to its equivalent Octal Number.
There are two programs:
1) Without using any predefine method and
2) Using Integer.toOctalString() method.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Without using any predefine method

// java program to convert decimal to octal

import java.util.*;

public class ConvDec2Oct {
  public static void main(String args[]) {
    int num, counter = 0;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter any integer number: ");
    num = sc.nextInt();

    //to store maximum 32 digits
    int octalVal[] = new int[32];

    while (num > 0) {
      octalVal[counter++] = num % 8;
      num = num / 8;
    }

    /*print octal values stored in octalVal*/
    for (int i = counter - 1; i >= 0; i--) {
      System.out.print(octalVal[i]);
    }
  }
}

Output:

Complie 	:	javac ConvDec2Oct.java
Run		:	java ConvDec2Oct
Output
Enter any integer number: 12345
30071

Using Integer.toOctalString() method

//java program to convert decimal to octal

import java.util.*;

public class ConvDec2Oct {
  public static void main(String args[]) {
    int num;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter any integer number: ");
    num = sc.nextInt();

    String str = Integer.toOctalString(num);
    System.out.println("Octal number is : " + str);
  }
}

Output:

Complie 	:	javac ConvDec2Oct.java
Run		:	java ConvDec2Oct
Output
Enter any integer number: 12345
Octal number is : 30071

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now