Sunday, July 26, 2015

JAVA - Conversion

import java.util.*; public class Conversion { public static void main(String args[]) { int num=0, decimal=0, binary=0, octal=0, hexa=0, i=0, a=0, x=0, rem=0, remain=0, result=0, power=0, binNumba=0, der=0, counter=0, ctr=0; String output = ""; char let=0; char ch; String binNum =" "; String octNum =" "; String hexNum =" "; String str = ""; char dig[] = {'0', '1', '2', '3', '4', '5', '6', '7'}; char diig[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C' ,'D', 'E', 'F'}; Scanner conVert = new Scanner(System.in); System.out.println("1.Decimal to Binary"); System.out.println("2.Decimal to Octal"); System.out.println("3.Decimal to Hexadecimal"); System.out.println("4.Binary to Decimal"); System.out.println("5.Binary to Octal"); System.out.println("6.Binary to Hexadecimal"); System.out.println("7.Octal to Decimal"); System.out.println("8.Octal to Binary"); System.out.println("9.Octal to Hexadecimal"); System.out.println("Enter the number of the specific conversion you want to have:"); num=conVert.nextInt(); if(num == 1)//decimal-binary { System.out.println("Enter a decimal:"); decimal=conVert.nextInt(); if(decimal<=0) System.out.println("Can`t be converted!"); else { binNum = " "; while(decimal!=0) { binNum = decimal%2 + binNum; decimal=decimal/2; } } System.out.println("Binary is: "+ binNum); } else if(num == 2)//decimal-octal { System.out.println("Enter a decimal:"); decimal=conVert.nextInt(); if(decimal<=0) System.out.println("Can`t be converted!"); else { octNum = " "; while(decimal!=0) { octNum = decimal%8 + octNum; decimal=decimal/8; } } System.out.println("Octal is: "+ octNum); } else if(num == 3)//decimal-hexadecimal { String tempHex = " "; System.out.println("Enter a decimal:"); decimal=conVert.nextInt(); if(decimal<0 an="" be="" converted="" decimal="" else="" hexnum=" " system.out.println="" t="" while="">=0) { result = decimal%16; if(result == 10) { tempHex = "A"; } else if(result == 11) { tempHex = "B"; } else if(result == 12) { tempHex = "C"; } else if(result == 13) { tempHex = "D"; } else if(result == 14) { tempHex = "E"; } else if(result == 15) { tempHex = "F"; } else { tempHex = ""+result; } hexNum = tempHex + hexNum; decimal=decimal - (result * 16); } } System.out.println("Hexa is: "+ hexNum); } else if(num == 4)//binary-decimal { System.out.println("Enter a binary:"); binNumba=conVert.nextInt(); if(binNumba < 0 ) { System.out.println("Invalid Input! A Binary Number is Either 1 or 0:"); binNumba=conVert.nextInt(); } else { while(binNumba != 0) { rem = binNumba % 10; decimal += rem * Math.pow(2,power); binNumba = binNumba/10; power++; } } System.out.println("Decimal is:" +decimal); } else if(num == 5)//binary-octal { System.out.println("\nEnter a binary number:"); binNumba = conVert.nextInt(); if(binNumba < 0) { System.out.println("Invalid Input! A Binary Number is Either 1 or 0:"); binNumba = conVert.nextInt(); } else { while(binary != 0) { rem = binNumba % 10; decimal += rem * Math.pow(2,power); binNumba = binNumba/10; power++; } while(decimal != 0) { remain = decimal % 8; str = dig[remain] + str; decimal = decimal/8; } System.out.println("The octal is:" +str); } } else if(num == 6)//binary-hexa { System.out.println("Invalid Input! A Binary Number is Either 1 or 0:"); binNumba = conVert.nextInt(); if(binary < 0) { System.out.println("Invalid Input! A Binary Number is Either 1 or 0:"); binNumba = conVert.nextInt(); } else { while(binary != 0) { rem = binary % 10; decimal += rem * Math.pow(2,power); binary = binary/10; power++; } while(decimal != 0) { rem = decimal % 16; str = diig[rem] + str; decimal = decimal/16; } System.out.println("The hexadecimal is:" +str); } } else if(num == 7)//octal-decimal { System.out.println("Enter an octal number:"); octal = conVert.nextInt(); if(octal < 0) { System.out.println("Invalid Input! Enter an Octal Number:"); octal=conVert.nextInt(); } else { while(octal != 0) { rem = octal % 10; decimal += rem * Math.pow(8,power); octal = octal/10; power++; } System.out.println("The decimal is:" +decimal); } } else if(num == 8)//octal-binary { System.out.println("Enter an octal number:"); octal = conVert.nextInt(); if(octal < 0) { System.out.println("Invalid Input! Enter an Octal Number:"); octal=conVert.nextInt(); } else { while(octal != 0) { rem = octal % 10; decimal += rem * Math.pow(8,power); octal = octal/10; power++; } while(decimal != 0) { counter = decimal % 2; ctr = counter; decimal /= 2; output = ctr +""+ output; } System.out.println("The decimal is:" +output); } } else if(num == 9)//octal-hexa { System.out.println("Enter an octal number:"); octal = conVert.nextInt(); if(octal < 0) { System.out.println("Invalid Input! Enter an Octal Number:"); octal=conVert.nextInt(); } else { while(octal != 0) { rem = octal % 10; decimal += rem * Math.pow(8,power); octal = octal/10; power++; } while(decimal != 0) { rem = decimal % 16; str = diig[rem] + str; decimal = decimal/16; } System.out.println("The decimal is:" +str); } }
}
}

No comments:

Post a Comment

google search

Custom Search