Showing posts with label Number System. Show all posts
Showing posts with label Number System. Show all posts

Java program for multiplication of two binary numbers.



package demo;
import java.util.Scanner;
public class BinaryMultiplication
{
 public static void main(String[] args)
 {
  long binary1, binary2, multiply = 0;
  int digit, factor = 1;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the first binary number: ");
  binary1 = sc.nextLong();
  System.out.print("Enter the second binary number: ");
  binary2 = sc.nextLong();
  while (binary2 != 0)
  {
   digit = (int)(binary2 % 10);
   if (digit == 1) 
   {
    binary1 = binary1 * factor;
    multiply = binaryproduct((int) binary1, (int) multiply);
   } 
   else
   {
    binary1 = binary1 * factor;
   }
   binary2 = binary2 / 10;
   factor = 10;
  }
  System.out.print("Product of two binary numbers: " + multiply);
 }

 static int binaryproduct(int binary1, int binary2) 
 {
  int i = 0, remainder = 0;
  int[] sum = new int[20];
  int binaryprod = 0;

  while (binary1 != 0 || binary2 != 0) 
  {
   sum[i++] = (binary1 % 10 + binary2 % 10 + remainder) % 2;
   remainder = (binary1 % 10 + binary2 % 10 + remainder) / 2;
   binary1 = binary1 / 10;
   binary2 = binary2 / 10;
  }
  if (remainder != 0)
  {
   sum[i++] = remainder;
  }
  --i;
  while (i >= 0) 
  {
   binaryprod = binaryprod * 10 + sum[i--];
  }
  return binaryprod;
 }
}

Output:
Enter the first binary number: 11100
Enter the second binary number: 11100
Product of two binary numbers: 1100010000
BUILD SUCCESSFUL (total time: 16 seconds)



Java program for addition of two binary numbers.



package demo;
import java.util.Scanner;
public class BinaryAddition 
{
 public static void main(String[] args)
 {
  long binary1, binary2;
  int i = 0, remainder = 0;
  int[] sum = new int[20];
  Scanner sc = new Scanner(System.in);

  System.out.print("Enter any first binary number: ");
  binary1 = sc.nextLong();
  System.out.print("Enter any second binary number: ");
  binary2 = sc.nextLong();

  while (binary1 != 0 || binary2 != 0) 
  {
   sum[i++] = (int)((binary1 % 10 + binary2 % 10 + remainder) % 2);
   remainder = (int)((binary1 % 10 + binary2 % 10 + remainder) / 2);
   binary1 = binary1 / 10;
   binary2 = binary2 / 10;
  }
  if (remainder != 0) {
   sum[i++] = remainder;
  }
  --i;
  System.out.print("Sum of two binary numbers: ");
  while (i >= 0) {
   System.out.print(sum[i--]);
  }
 }
}

Output:
Enter any first binary number: 111111
Enter any second binary number: 101010
Sum of two binary numbers: 1101001
BUILD SUCCESSFUL (total time: 8 seconds)


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)


Java program to convert decimal to binary.



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

Output:
Enter any decimal number: 100
Equivalent binary value of decimal number 100 =  1100100
BUILD SUCCESSFUL (total time: 3 seconds)


Algorithum
Step 1: Divide the original decimal number by 2
Step 2: Divide the quotient by 2
Step 3: Repeat the step 2 until we get quotient equal to zero.