This program is written for to convert octal number to decimal number programmatically. It is a important interview question and this program as logic to convert octal to decimal.
First we find the given number is octal or not. Then we will write logic for decimal number from octal. Observe below logic how to write logic to convert octal to decimal.
package com.rrv;
import java.math.*;
import java.util.Scanner;
//class for converting Octal to Decimal
class OctalToDecimal
{
//main method
public static void main(String[] args)
{
//declare boolean type variable for to check given number is octal or not
boolean is_Octal=false;
//create scanner object for taking values from user
Scanner scanner=new Scanner(System.in);
System.out.println("Enter Octal Number ");
int octalNum=scanner.nextInt();
//call isOctal() method for finding given number is octal or not
is_Octal= isOctal(octalNum);
//check given number is octal or not,if true call octalToDecimal() method else print message to console
if (is_Octal==true){
octalToDecimal(octalNum);
}
else
{
System.out.println("Entered Number is not octal number. Please enter Octal number");
}
}
// method for finding given number is octal or not
public static boolean isOctal(int octalNum)
{
//declare remainder variable for to store remainder value
int remain=0;
//if octal number is > zero then enter into while loop
while(octalNum>0)
{
remain=octalNum%10;
if(remain<8)
{
octalNum=octalNum/10;
}
else{
return false;
}
}
return true;
}
//method for convert into decimal
public static void octalToDecimal(int octalNum)
{
int inputNumber=octalNum;
int remain=0;
int count=0;
int decimal=0;
while(inputNumber>0)
{
remain=inputNumber%10;
//first find power value of 8 with count and multiply with remainder then add to decimal variable
decimal=(int) (decimal+remain*Math.pow(8,count));
count++;
inputNumber=inputNumber/10;
}
System.out.println("The Decimal number for "+octalNum+" octal is: "+decimal);
}
}
OutPut:
Enter Octal Number
173
The Decimal number for 173 octal is: 123
Enter Octal Number
189
Entered Number is not octal number. Please enter Octal number
First we find the given number is octal or not. Then we will write logic for decimal number from octal. Observe below logic how to write logic to convert octal to decimal.
package com.rrv;
import java.math.*;
import java.util.Scanner;
//class for converting Octal to Decimal
class OctalToDecimal
{
//main method
public static void main(String[] args)
{
//declare boolean type variable for to check given number is octal or not
boolean is_Octal=false;
//create scanner object for taking values from user
Scanner scanner=new Scanner(System.in);
System.out.println("Enter Octal Number ");
int octalNum=scanner.nextInt();
//call isOctal() method for finding given number is octal or not
is_Octal= isOctal(octalNum);
//check given number is octal or not,if true call octalToDecimal() method else print message to console
if (is_Octal==true){
octalToDecimal(octalNum);
}
else
{
System.out.println("Entered Number is not octal number. Please enter Octal number");
}
}
// method for finding given number is octal or not
public static boolean isOctal(int octalNum)
{
//declare remainder variable for to store remainder value
int remain=0;
//if octal number is > zero then enter into while loop
while(octalNum>0)
{
remain=octalNum%10;
if(remain<8)
{
octalNum=octalNum/10;
}
else{
return false;
}
}
return true;
}
//method for convert into decimal
public static void octalToDecimal(int octalNum)
{
int inputNumber=octalNum;
int remain=0;
int count=0;
int decimal=0;
while(inputNumber>0)
{
remain=inputNumber%10;
//first find power value of 8 with count and multiply with remainder then add to decimal variable
decimal=(int) (decimal+remain*Math.pow(8,count));
count++;
inputNumber=inputNumber/10;
}
System.out.println("The Decimal number for "+octalNum+" octal is: "+decimal);
}
}
OutPut:
Enter Octal Number
173
The Decimal number for 173 octal is: 123
Enter Octal Number
189
Entered Number is not octal number. Please enter Octal number
No comments:
Post a Comment