Skip to main content

Posts

Showing posts with the label decimal

binary to decimal conversion code in java

 binary to decimal conversion code in java Code: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static void main(String[] args){ Scanner scn = new Scanner(System.in); System.out.println("Enter the Binary Number : "); int bin = scn.nextInt(); deci(bin); } public static void deci(int bin){ int res = 0; int i = 0; while(bin!=0){ int r = bin%10; res = res + (r*(int)Math.pow(2,i)); i++; bin = bin/10; } System.out.println(res); } } ex: input : 1010 o/p:  10 input : 1000 o/p: 8 i/p: 10101 o/p: 21

Decimal to Hexadecimal Conversion code in java

 Decimal to Hexadecimal Conversion Code:: import java.util.*; import java.lang.*; import java.io.*; class Test{ public static void main(String[] args){ Scanner scn = new Scanner(System.in); int dec = scn.nextInt(); hexa(dec); } public static void hexa(int dec){ String hexa = "0123456789ABCDEF"; String res = ""; while(dec!=0){ int r = dec%16; res = hexa.charAt(r) + res; dec = dec/16; } System.out.println(res); } } ex:: hexadecimal Number :: F Decimal Number :: 15 hexadecimal number : 1A decimal number : 26