Import Import Import Public Class: Out Out Out Out Out
This program converts integers to binary, decimal, hexadecimal, and octal representations. It takes an integer as input from the user and their choice of representation. It uses a commonConvert method that divides the integer by the given size and stores the remainders in an array, then reverses the array to get the output number. It has separate methods to display the output in binary, hexadecimal, or octal format by looping through the array.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
44 views4 pages
Import Import Import Public Class: Out Out Out Out Out
This program converts integers to binary, decimal, hexadecimal, and octal representations. It takes an integer as input from the user and their choice of representation. It uses a commonConvert method that divides the integer by the given size and stores the remainders in an array, then reverses the array to get the output number. It has separate methods to display the output in binary, hexadecimal, or octal format by looping through the array.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4
/*
-this program is used to convert any integer value into
binary,decimal,hexadecimal and octal
LOGIC B!I"# CO"$%&IO" -integer to binary '(#ivide the integer by ) until the remainder reaches *+*( )(%everse the obtained remainders to get the binary value( -integer to decimal '(in programming languages all the integer values are decimal values -integer to hexadecimal '(#ivide the number by ', and store the remainder hexadecimal value say -( )(.a/e the 0uotient and again divide it by ',,store the remainder in hexadecimal 1ormat in -(#o this process till 0uotient reaches belo2 ',( 3(%everse the stored - value( -integer to octal &ame process li/e hexadecimal but the number should be divided by 4(
5rocedure '(ta/e the integer value as input( )(convert the number into desired 1ormat( 3(#isplay the number
*/ import 6ava(io(Bu11ered%eader7 import 6ava(io(Input&tream%eader7 import 6ava(io(IOxception7 public class Convert 8 public static void main9&tring:; input<romCommand= thro2s IOxception 8 CommonConverter cc> ne2 CommonConverter9=7 char again > +7 do 8 &ystem(out(println9?@elcome to number convertor 2here decimal number can be converted to 1ollo2ing 1ormatsA?=7 &ystem(out(println9?'(to binary 1ormat?=7 &ystem(out(println9?)(to hexadecimal 1ormat?=7 &ystem(out(println9?3( to octal 1ormat?=7 &ystem(out(println9?nter your number 1ollo2ed by the choice?=7 int number,choice7 Bu11ered%eader br>ne2 Bu11ered%eader9ne2 Input&tream%eader9&ystem(in==7 number> Integer(parseInt9br(readLine9==7 //ta/ing decimal number as input choice>Integer(parseInt9br(readLine9==7 //user*s choice s2itch9choice= 8 case 'Acc(binaryConvert9number,)=7 brea/7 case )Acc(hexadecimalConverter9number,', =7 brea/7 case 3Acc(octalConvert9number, 4=7 brea/7 de1aultA&ystem(out(println9?use ' 1or binary,) 1or hexadecimal and 3 1or octal?=7 &ystem(out(println9?2ant to try again9y/n=?=7 again>9char=br(read9=7 B B 2hile9again>>*y*=7 B B public class CommonConverter 8 int commonLength>+7 public int:; commonConvert9int decimalInput,int siCe= 8 int common:;> ne2 int:3);7 //array o1 3) bit siCe to store the converted number int n>',temp7 1or9int i>+7iDn7iEE= 8 common:i;>decimalInputFsiCe7 //storing the remainder decimalInput>decimalInput/siCe7 //dividing the number by siCe this(commonLength>this(commonLengthE'7 //to count ho2 many digits are stored in array i19decimalInput>>+= 8 n>n-'7 //i1 decimalInput reaches + end the loop B else 8 n>nE'7 B B 1or9int i>+,6>this(commonLength- '7iD9this(commonLength/)=7iEE,6--= //reversing the number to get output 8 temp>common:i;7 common:i;>common:6;7 common:6;>temp7 B return common7 B public void hexadecimalConverter9int decimalInput,int siCe= //hexadecimal conversion 8 int temphex7 int:; hex > commonConvert9decimalInput,siCe=7 &ystem(out(print9?!exadecimal code isA ?=7 1or9int i>+7iDthis(commonLength7iEE= 8 i19hex:i;GH= //converting the digit 2hich are more then H to - B C # < 8 temphex>hex:i;-'+7 hex:i;>*-*Etemphex7 &ystem(out(print99char=hex:i;=7 B else 8 &ystem(out(print9hex:i;=7 //printing the numbers B B this(commonLength>+7 B public void binaryConvert9int decimalInput,int siCe= //binary conversion 8 int:; binary>commonConvert9decimalInput,siCe=7 &ystem(out(print9?Binary code isA ?=7 1or9int i>+7iDthis(commonLength7iEE= 8 &ystem(out(print9binary:i;=7 B this(commonLength>+7 B public void octalConvert9int decimalInput,int siCe= //octal conversion 8 int:; binary>commonConvert9decimalInput,siCe=7 &ystem(out(print9?octal code isA ?=7 1or9int i>+7iDthis(commonLength7iEE= 8 &ystem(out(print9binary:i;=7 B this(commonLength>+7 B B