**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