0% found this document useful (0 votes)
12 views12 pages

PR

The document is a Java program that provides a menu-driven interface for converting numbers between different numeral systems, including binary, octal, decimal, and hexadecimal. It includes methods for converting from decimal to binary, octal, and hexadecimal, as well as for converting from binary, octal, and hexadecimal back to decimal. The program continues to prompt the user for conversions until the user chooses to exit.

Uploaded by

tanviivnat04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

PR

The document is a Java program that provides a menu-driven interface for converting numbers between different numeral systems, including binary, octal, decimal, and hexadecimal. It includes methods for converting from decimal to binary, octal, and hexadecimal, as well as for converting from binary, octal, and hexadecimal back to decimal. The program continues to prompt the user for conversions until the user chooses to exit.

Uploaded by

tanviivnat04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

import java.util.

*;
public class Project4
{
public static String conversionFromDecimalToBinaryOrOctal(int a
,int val)
{
String d="";
while(a>0)
{
int b=a/val;
int re=a%val;
d=re+d;
a=b;
re=0;
}
return d;
}
public static int fromBinaryOrOctalToDecimal(String a,int val)
{
int num=Integer.parseInt(a);
double d=0;int j=0;
while(num>0)
{
int b=num%10;
d=b*Math.pow(val,j)+d;
num=num/10;
j++;
}
return (int) d;
}
public static String conversionFromDecimalToHexadecimal(int a)
{
int aj[]={10,11,12,13,14,15};
char bj[]={'A','B','C','D','E','F'};
String d="";
while(a>0)
{
int b=a/16;
int re=a%16;
for(int i=0;i<6;i++)
{
if(re==aj[i])
{
d=bj[i]+d;
break;
}
else if(re<10)
{
d=re+d;
break;
}
}
a=b;
re=0;
}
return d;
}
public static int fromHexadecimalToDecimal(String h)
{
String hexa=h.toUpperCase();
String str="0123456789ABCDEF";
double num=0;int j=0;
for(int i=hexa.length()-1;i>=0;i--)
{
char ch=hexa.charAt(i);
int n=str.indexOf(ch);
num =n*Math.pow(16,j)+num;
j++;
}
return (int) num;
}
public static void main(String args[])
{
int choice=1;
while(choice==1)
{
System.out.println("1-From Decimal number to Binary
Number.");
System.out.println("2-From Binary Number to Decimal
number.");
System.out.println("3-From Decimal number to Octal
Number.");
System.out.println("4-From Octal number to Decimal
number.");
System.out.println("5-From Decimal number to Hexadecimal
Number.");
System.out.println("6-From Hexadecimal Number to Decimal
Number.");
System.out.println("7-From Octal number to Hexadecimal
number.");
System.out.println("8-From Hexadecimal number to Octal
number");
System.out.println("0-To exit");
System.out.println("Enter the number corresponding to your
choice.");
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice:-");
int c=sc.nextInt();
while(c%1!=0 || c>8)
{
System .out.println("You have entered the wrong
choice.Please again enter your choice :-");
c=sc.nextInt();
}
switch(c)
{
case 1:
{
System.out.println("Enter the Decimal (positive integer)
number:-");
int num=sc.nextInt();
while(num<0)
{
System.out.println("Enter a positive integer");
num=sc.nextInt();
}
String b=conversionFromDecimalToBinaryOrOctal(num,2);
System.out.println("Its binary form = "+"("+b+")"+" base
2");
}
break;
case 2:
{
System.out.println("Enter the positive Binary(integer)
number.");
String num=sc.next();
int leng=num.length();
for(int i=0;i<leng;i++)
{
while(num.charAt(0)=='-' || num.charAt(i)=='0' &
num.charAt(i)=='1')
{
System.out.println("Enter the positive Binary
number.");
num=sc.next();
leng=num.length();
i=0;
}
}
int b=fromBinaryOrOctalToDecimal(num,2);
System.out.println("Its decimal form = "+"("+b+")"+"base
10");
}
break;
case 3:
{
System.out.println("Enter the Decimal (positive integer)
number:-");
int num=sc.nextInt();
while(num<0)
{
System.out.println("Enter a positive integer");
num=sc.nextInt();
}
String b=conversionFromDecimalToBinaryOrOctal(num,8);
System.out.println("Its octal form = "+"("+b+")"+"base
8");
}
break;
case 4:
{
System.out.println("Enter the positive Octal(integer)
number.");
String num=sc.next();
int leng=num.length();
for(int i=0;i<leng;i++)
{
while(num.charAt(0)=='-' || num.charAt(i)=='8' ||
num.charAt(i)=='9')
{
System.out.println("Enter the correct positive
Octal number.");
num=sc.next();
leng=num.length();
i=0;
}
}
int b=fromBinaryOrOctalToDecimal(num,8);
System.out.println("Its decimal form =
"+"("+b+")"+"base 10");
}
break;
case 5:
{
System.out.println("Enter the Decimal (positive integer)
number:-");
int num=sc.nextInt();
while(num<0)
{
System.out.println("Enter a positive integer");
num=sc.nextInt();
}
String b=conversionFromDecimalToHexadecimal(num);
System.out.println("Its Hexadecimal form =
"+"("+b+")"+"base 16");
}
break;
case 6:
{
System.out.println("Enter the positive Hexadecimal
number.");
String num=sc.next();
int leng=num.length();
while(num.charAt(0)=='-')
{
System.out.println("Enter the correct positive
Hexadecimal number.");
num=sc.next();
leng=num.length();
}
int b=fromHexadecimalToDecimal(num);
System.out.println("Its decimal form =
"+"("+b+")"+"base 10");
}
break;
case 7:
{
System.out.println("Enter the positive Octal(integer)
number.");
String num=sc.next();
int leng=num.length();
for(int i=0;i<leng;i++)
{
while(num.charAt(0)=='-' || num.charAt(i)=='8' ||
num.charAt(i)=='9')
{
System.out.println("Enter the correct positive
Octal number.");
num=sc.next();
leng=num.length();
i=0;
}
}
int b=fromBinaryOrOctalToDecimal(num,8);
String d=conversionFromDecimalToHexadecimal(b);
System.out.println("Its Hexadecimal form =
"+"("+d+")"+"base 16");
}
break;
case 8:
{
System.out.println("Enter the positive Hexadecimal
number.");
String num=sc.next();
int leng=num.length();
while(num.charAt(0)=='-')
{
System.out.println("Enter the correct positive
Hexadecimal number.");
num=sc.next();
leng=num.length();
}
int b=fromHexadecimalToDecimal(num);
String d =conversionFromDecimalToBinaryOrOctal(b,8);
System.out.println("Its octal form = "+"("+d+")"+"base
8");
}
break;
case 0:
System.exit(0);
}
System.out.println("If you want to continue , please enter 1
or if you want to exit enter 0.");
choice =sc.nextInt();
if(choice == 0)
{
System.out.println("You are exited.");
}
else
{
System.out.println("Re-enter your choice = ");
choice =sc.nextInt();
}
}
}
}

You might also like