ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Java Program to convert number from Decimal to Octal.

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.

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]);
		    }
       }
    }
    
    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);
       }
    }
    
    Complie 	:	javac ConvDec2Oct.java
    Run		:	java ConvDec2Oct
    Output
    Enter any integer number: 12345
    Octal number is : 30071
    



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.