Pages

Friday, 19 August 2016

Chapter 5 Exercise 38, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**5.38 (Decimal to octal) Write a program that prompts the user to enter a decimal integer and displays its corresponding octal value. Don’t use Java’s Integer .toOctalString(int) in this program.

import java.util.Scanner;
 
 
public class ProgrammingEx5_38 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter an integer:");
  int d = input.nextInt();
  String oct = "";
 
  while (d != 0) {
   oct = d % 8 + oct;
   d = d / 8;
  }
 
  System.out.println("The octal form is " + oct);
 
 }
 
}

No comments:

Post a Comment