Showing posts with label Conversion. Show all posts
Showing posts with label Conversion. Show all posts

Java program for binary to hexadecimal conversion.



package demo;
import java.util.Scanner;
public class BinaryToHexadecimal
{
 public static void main(String[] args)
 {
  int[] hex = new int[1000];
  int i = 1, j = 0, rem, dec = 0, bin;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter a Binary Number: ");
  bin = sc.nextInt();
  while (bin > 0) {
   rem = bin % 2;
   dec = dec + rem * i;
   i = i * 2;
   bin = bin / 10;
  }
  /* At this state our input number is converted into Decimal Number */
  i = 0;
  while (dec != 0) {
   hex[i] = dec % 16;
   dec = dec / 16;
   i++;
  }
  System.out.print("Equivalent HexaDecimal value: ");
  for (j = i - 1; j >= 0; j--)
  {
   if (hex[j] > 9) 
   {
    System.out.print((char)(hex[j] + 55));
   } else
   {
    System.out.print(hex[j]);
   }
  }
 }
}

Output:
Enter a Binary Number: 11111100
Equivalent HexaDecimal value: FC
BUILD SUCCESSFUL (total time: 6 seconds)


Java program for binary to decimal conversion.



import java.util.Scanner;
public class BinaryToDecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  long binaryNumber, octalNumber = 0, j = 1, remainder;
  System.out.print("Enter any number any binary number: ");
  binaryNumber = sc.nextLong();

  while (binaryNumber != 0) 
  {
   remainder = binaryNumber % 10;
   octalNumber = octalNumber + remainder * j;
   j = j * 2;
   binaryNumber = binaryNumber / 10;
  }
  System.out.println("Octal Number: " + octalNumber);
 }
}

Output:
Enter any number any binary number: 1100110
Octal Number: 102
BUILD SUCCESSFUL (total time: 5 seconds)



Java program for octal to decimal.



package demo;
import java.util.Scanner;
public class OctalToDecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  long octal, decimal = 0;
  int i = 0;
  System.out.print("Enter any octal number: ");
  octal = sc.nextLong();
  while (octal != 0) 
  {
   decimal = (long)(decimal + (octal % 10) * Math.pow(8, i++));
   octal = octal / 10;
  }
  System.out.print("Equivalent decimal value: " + decimal);
 }
}

Output:
Enter any octal number: 1000
Equivalent decimal value: 512
BUILD SUCCESSFUL (total time: 3 seconds)


Java program to convert decimal to hexadecimal number.



package demo;
import java.util.Scanner;
public class DecimalToHexadecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int decimalNumber, remainder, quotient;
  int i = 1, temp;
  byte[] hexadecimalNumber = new byte[100];
  System.out.print("Enter any decimal number: ");
  decimalNumber = sc.nextInt();
  quotient = decimalNumber;
  while (quotient != 0) 
  {
   temp = (quotient % 16);              //To convert integer into character
   if (temp < 10) 
   {
    temp = temp + 48;
   } else 
   {
    temp = temp + 55;
   }
   hexadecimalNumber[i++] = (byte) temp;
   quotient = quotient / 16;
  }
  System.out.print("Equivalent hexadecimal value of decimal number " + ( decimalNumber) + ": ");
  for (int j = i - 1; j > 0; j--) 
  {
   System.out.print((char)(hexadecimalNumber[j]));
  }
 }
}

Output:
Enter any decimal number: 1000
Equivalent hexadecimal value of decimal number 1000: 3E8
BUILD SUCCESSFUL (total time: 3 seconds)



Java program to convert decimal to octal number.



package demo;
import java.util.Scanner;
public class DecimalToOctal 
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int decimalNumber, remainder, quotient;
  int[] octalNumber = new int[100];
  int i = 1;
  System.out.print("Enter any decimal number: ");
  decimalNumber = sc.nextInt();
  quotient = decimalNumber;
  while (quotient != 0) 
  {
   octalNumber[i++] = (quotient % 8);
   quotient = quotient / 8;
  }
  System.out.print("Equivalent octal value of decimal number " + ( decimalNumber) + ": ");
  for (int j = i - 1; j > 0; j--) {
   System.out.print(octalNumber[j]);
  }
 }
}

Output:
Enter any decimal number: 100
Equivalent octal value of decimal number 100: 144
BUILD SUCCESSFUL (total time: 4 seconds)