Java Program to Convert Number From Decimal to Octal

Java Program to Convert Number From Decimal to Octal

Java Program to Convert Number From Decimal to Octal

public class DecimalToOctalExample1{  
public static void main(String args[]){  
System.out.println(Integer.toOctalString(8));  
System.out.println(Integer.toOctalString(19));  
System.out.println(Integer.toOctalString(81));  
}}    
public class DecimalToOctalExample2{    
public static String toOctal(int decimal){    
    int rem;  
    String octal="";   
    char octalchars[]={'0','1','2','3','4','5','6','7'};  
   
    while(decimal>0)  
    {  
       rem=decimal%8;   
       octal=octalchars[rem]+octal;   
       decimal=decimal/8;  
    }  
    return octal;  
}    
public static void main(String args[]){      
System.out.println("Decimal of octal 8 is: "+toOctal(8));  
System.out.println("Decimal of octal 19 is: "+toOctal(19));  
System.out.println("Decimal of octal 81 is: "+toOctal(81));  
}} 
10
23
121
Decimal to octal of 8 is: 10
Decimal to octal of 19 is: 23
Decimal to octal of 81 is: 121

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment