Question

In: Computer Science

Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...

Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent.

If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program.

The output of your program will always be a 7-digit octal number with no spaces between any of the digits. Some of the leading digits may be 0.

Use a while loop to solve the problem. Do not use strings.

Sample Program Run
Please enter a number between 0 and 2097151 to convert: 160000
Your integer number 160000 is 0470400 in octal.

Please enter a number between 0 and 2097151 to convert: 5000000
UNABLE TO CONVERT

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


// DecimalToOctal.java

import java.util.Scanner;

public class DecimalToOctal {

   public static void main(String[] args) {
       int decimal,octal;
       String octalStr="",str="";
      
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       System.out.print("Please enter a number between 0 and 2097151 :");
       decimal=sc.nextInt();
       if(decimal<0 || decimal>2097151)
       {
           System.out.println("UNABLE TO CONVERT");
       }
       else
       {
           octal=decimalToOctal(decimal);
          
           if(String.valueOf(octal).length()<7)
           {
               for(int i=0;i<7-String.valueOf(octal).length();i++)
               {
                   str+="0";
               }
              
           }
          
           System.out.println("Your integer number "+decimal+" is "+str+""+octal+" in octal.");
          
       }
   }
   //This function will convert decimal to octal form
   public static int decimalToOctal(int decimal_num)
   {
       int rem=0;
   int octal=0;
   String str="";
   while(decimal_num!=0)
   {
      rem=decimal_num%8;
      str+=rem;
      decimal_num=decimal_num/8;  
   }
     
   String rev="";
   for(int i=str.length()-1;i>=0;i--)
   {
      rev=rev+str.charAt(i);
   }
     
   return Integer.parseInt(rev);  
   }

}
__________________

Output:

Please enter a number between 0 and 2097151 :160000
Your integer number 160000 is 0470400 in octal.

__________________

Output#2:

Please enter a number between 0 and 2097151 :5000000
UNABLE TO CONVERT

_______________Could you plz rate me well.Thank You


Related Solutions

Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of...
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of each of these lists to generate a total sum Print the lists, and the total sum of each value C++ contains some built-in functions (such as itoa and std::hex) which make this assignment trivial. You may NOT use these in your programs. You code must perform the conversion through your own algorithm. The input values are: 5 9 24 2 39 83 60 8...
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers....
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers. The program should be run from the command prompt, output a text prompt to the screen, and then wait for the user to type in a number followed by the Enter key. (The legitimate range of user input values is any signed integer that can be represented in 32 bits.) After each number is entered, the program should determine and display the following information...
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
convert the binary number(base 2) To Octal (base 8) to decimal (base 10) a. 101 b....
convert the binary number(base 2) To Octal (base 8) to decimal (base 10) a. 101 b. 1001 c. 101010 d.1101101 convert the number to the other base a. 253 base 10 to base 8 b. 98 base 10 to base 3 C. 1340 base 10 to base 16 D. AB Base 16 to base 8 E. 111010 base 2 to base 16 F. 1010101 base 2 to base 6 g. 69 base 10 to base 2 h . 1023 base...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT