Search Tutorials

Loading...

Tuesday, 23 April 2013

Java code to convert Decimal to Octal

/*
Program to convert Decimal to Octal.
*/
import java.io.*;
class decimal2octal
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
decimal2octal call = new decimal2octal();
System.out.print("Enter a decimal number : ");
int n = Integer.parseInt(br.readLine());
System.out.print("\nOctal Equivalent : ");
call.dec2oct(n);
}
void dec2oct(int n)
{
int c;
if(n!=0)
{
c=n%8;
dec2oct(n/8);
System.out.print(c);
}
}
}


/**
* ALGORITHM :
* ---------
* 1. Start
* 2. Accept a decimal number from the user.
* 3. Using recursion, convert it to octal.
* 4. Print the Octal Equivalent.
* 5. End
*/

/*
OUTPUT :
------
Enter a decimal number : 25
Octal Equivalent : 31
*/


No comments:

Post a Comment

Back to Top