Header Ads Widget

Converter program in JAVA

 1. Convert Binary to Decimal 


class Main {
    
  public static void main(String[] args) {
    long num = 110110111;
    int decimal = convertBinaryToDecimal(num);
    System.out.println("Binary to Decimal");
    System.out.println(num + " = " + decimal);
  }

  public static int convertBinaryToDecimal(long num) {
    int decimalNumber = 0, i = 0;
    long remainder;
    
    while (num != 0) {
      remainder = num % 10;
      num /= 10;
      decimalNumber += remainder * Math.pow(2, i);
      ++i;
    }
    
    return decimalNumber;
  }
}


Output :-


Binary to Decimal
1010110 = 86



2. Convert Decimal to Binary 


class Main {

  public static void main(String[] args) {
    int num = 10;
    System.out.println("Decimal to Binary");
    long binary = convertDecimalToBinary(num);
    System.out.println("\n" + num + " = " + binary);
    }

  public static long convertDecimalToBinary(int n) {
    long binaryNumber = 0;
    int remainder, i = 1, step = 1;

    while (n!=0) {
      remainder = n % 2;
        System.out.println("Step " + step++ + ": " + n + "/2");
        System.out.println("Quotient = " + n/2 + ", Remainder = " + remainder);
        n /= 2;
        binaryNumber += remainder * i;
        i *= 10;
    }
    
    return binaryNumber;
    }
}


Output :-


Decimal to Binary
Step 1: 10/2
Quotient = 5, Remainder = 0
Step 2: 5/2
Quotient = 2, Remainder = 1
Step 3: 2/2
Quotient = 1, Remainder = 0
Step 4: 1/2
Quotient = 0, Remainder = 1
10 = 1010



3. Convert Binary to octal


class Main {
  public static void main(String[] args) {
    long binary = 1010110;
    int octal = convertBinarytoOctal(binary);
    System.out.println(binary + " in binary = " + octal + " in octal");
  }

  public static int convertBinarytoOctal(long binaryNumber) {
    int octalNumber = 0, decimalNumber = 0, i = 0;

    while (binaryNumber != 0) {
      decimalNumber += (binaryNumber % 10) * Math.pow(2, i);
      ++i;
      binaryNumber /= 10;
    }

    i = 1;

    while (decimalNumber != 0) {
      octalNumber += (decimalNumber % 8) * i;
      decimalNumber /= 8;
      i *= 10;
    }

    return octalNumber;
  }
}


Output :-


1010110 in binary = 126 in octal



4.  Convert Octal to Binary


class Main {
  public static void main(String[] args) {
    int octal = 86;
    long binary = convertOctalToBinary(octal);
    System.out.println(octal + " in octal = " + binary + " in binary");
  }

  public static long convertOctalToBinary(int octalNumber) {
    int decimalNumber = 0, i = 0;
    long binaryNumber = 0;

    while (octalNumber != 0) {
      decimalNumber += (octalNumber % 10) * Math.pow(8, i);
      ++i;
      octalNumber /= 10;
    }

    i = 1;

    while (decimalNumber != 0) {
      binaryNumber += (decimalNumber % 2) * i;
      decimalNumber /= 2;
      i *= 10;
    }

    return binaryNumber;
  }
}


Output :-


86 in octal = 1000110 in binary


5. Convert Decimal to Octal


public class DecimalOctal {

    public static void main(String[] args) {
        int decimal = 86;
        int octal = convertDecimalToOctal(decimal);
        System.out.printf("%d in decimal = %d in octal", decimal, octal);
    }

    public static int convertDecimalToOctal(int decimal)
    {
        int octalNumber = 0, i = 1;

        while (decimal != 0)
        {
            octalNumber += (decimal % 8) * i;
            decimal /= 8;
            i *= 10;
        }

        return octalNumber;
    }
}


Output :-


86 in decimal = 126 in octal



6. Convert Octal to Decimal


public class OctalDecimal {

    public static void main(String[] args) {
        int octal = 86;
        int decimal = convertOctalToDecimal(octal);
        System.out.printf("%d in octal = %d in decimal", octal, decimal);
    }

    public static int convertOctalToDecimal(int octal)
    {
        int decimalNumber = 0, i = 0;

        while(octal != 0)
        {
            decimalNumber += (octal % 10) * Math.pow(8, i);
            ++i;
            octal/=10;
        }

        return decimalNumber;
    }
}


Output :-


86in octal = 70 in decimal


7. Convert Inches to Meters


import java.util.Scanner;
public class Exercise2 {

    public static void main(String[] Strings) {

        Scanner input = new Scanner(System.in);

        System.out.print("Input a value for inch: ");
        double inch = input.nextDouble();
        double meters = inch * 0.0254;
        System.out.println(inch + " inch is " + meters + " meters");

    }
}


Output :-



Input a value for inch: 500
500.0 inch is 12.7 meters



8. Convert Fahrenheit to Celsius


import java.util.Scanner;
public class Exercise1 {

    public static void main(String[] Strings) {

        Scanner input = new Scanner(System.in);

        System.out.print("Input a degree in Fahrenheit: ");
        double fahrenheit = input.nextDouble();

        double  celsius =(( 5 *(fahrenheit - 32.0)) / 9.0);
        System.out.println(fahrenheit + " degree Fahrenheit is equal to " + celsius + " in Celsius");
    }
}


Output :-


Input a degree in Fahrenheit: 300
300.0 degree Fahrenheit is equal to 148.88888888888889 in Celsius



9. Currency Converter


import java.util.*;
import java.text.DecimalFormat;
 
public class CurrencyConverter {
 
public static void main(String[] args) {
 
double amount, dollar, pound, code, euro, yen, ringgit, rupee;
 
DecimalFormat f = new DecimalFormat("##.##");
 
Scanner sc = new Scanner(System.in);
 
System.out.println("hi, Welcome to the Currency Converter!");
 
System.out.println("which currency You want to Convert ? ");
System.out.println("1:Ruppe \t2:Dollar \t3:Pound \n4:Euro \t5:Yen \t6:Ringgit ");
code = sc.nextInt();
System.out.println("How much Money you want to convert ?");
amount = sc.nextFloat();
 
// For amounts Conversion
if (code == 1) {
 
dollar = amount / 70;
System.out.println("Your " + amount + " Rupee is : " + f.format(dollar) + " Dollar");
 
pound = amount / 88;
System.out.println("Your " + amount + " Rupee is : " + f.format(pound) + " Pound");
 
euro = amount / 80;
System.out.println("Your " + amount + " Rupee is : " + f.format(euro) + " Euro");
 
yen = amount / 0.63;
System.out.println("Your " + amount + " Rupee is : " + f.format(yen) + " Yen");
 
ringgit = amount / 16;
System.out.println("Your " + amount + " Rupee is : " + f.format(ringgit) + " ringgit");
} else if (code == 2) {
// For Dollar Conversion
 
rupee = amount * 70;
System.out.println("Your " + amount + " Dollar is : " + f.format(rupee) + " Ruppes");
 
pound = amount * 0.78;
System.out.println("Your " + amount + " Dollar is : " + f.format(pound) + " Pound");
 
euro = amount * 0.87;
System.out.println("Your " + amount + " Dollar is : " + f.format(euro) + " Euro");
 
yen = amount * 111.087;
System.out.println("Your " + amount + " Dollar is : " + f.format(yen) + " Yen");
 
ringgit = amount * 4.17;
System.out.println("Your " + amount + " Dollar is : " + f.format(ringgit) + " ringgit");
} else if (code == 3) {
// For Pound Conversion
 
rupee = amount * 88;
System.out.println("Your " + amount + " pound is : " + f.format(rupee) + " Ruppes");
 
dollar = amount * 1.26;
System.out.println("Your " + amount + " pound is : " + f.format(dollar) + " Dollar");
 
euro = amount * 1.10;
System.out.println("Your " + amount + " pound is : " + f.format(euro) + " Euro");
 
yen = amount * 140.93;
System.out.println("Your " + amount + " pound is : " + f.format(yen) + " Yen");
 
ringgit = amount * 5.29;
System.out.println("Your " + amount + " pound is : " + f.format(ringgit) + " ringgit");
} else if (code == 4) {
// For Euro Conversion
 
rupee = amount * 80;
System.out.println("Your " + amount + " euro is : " + f.format(rupee) + " Ruppes");
 
dollar = amount * 1.14;
System.out.println("Your " + amount + " euro is : " + f.format(dollar) + " Dollar");
 
pound = amount * 0.90;
System.out.println("Your " + amount + " euro is : " + f.format(pound) + " Pound");
 
yen = amount * 127.32;
System.out.println("Your " + amount + " euro is : " + f.format(yen) + " Yen");
 
ringgit = amount * 4.78;
System.out.println("Your " + amount + " euro is : " + f.format(ringgit) + " ringgit");
} else if (code == 5) {
 
// For Yen Conversion
 
rupee = amount * 0.63;
System.out.println("Your " + amount + " yen is : " + f.format(rupee) + " Ruppes");
 
dollar = amount * 0.008;
System.out.println("Your " + amount + " yen is : " + f.format(dollar) + " Dollar");
 
pound = amount * 0.007;
System.out.println("Your " + amount + " yen is : " + f.format(pound) + " Pound");
 
euro = amount * 0.007;
System.out.println("Your " + amount + " yen is : " + f.format(euro) + " Euro");
 
ringgit = amount * 0.037;
System.out.println("Your " + amount + " yen is : " + f.format(ringgit) + " ringgit");
} else if (code == 6) {
// For Ringgit Conversion
 
rupee = amount * 16.8;
System.out.println("Your " + amount + " ringgit is : " + f.format(rupee) + " Ruppes");
 
dollar = amount * 0.239;
System.out.println("Your " + amount + " ringgit is : " + f.format(dollar) + " dollar");
 
pound = amount * 0.188;
System.out.println("Your " + amount + " ringgit is : " + f.format(pound) + " pound");
 
euro = amount * 0.209;
System.out.println("Your " + amount + " ringgit is : " + f.format(euro) + " euro");
 
yen = amount * 26.63;
System.out.println("Your " + amount + " ringgit is : " + f.format(yen) + " yen");
} else {
System.out.println("Invalid input");
}
System.out.println("Thank you for choosing our Anonymous Programs");
}
 
}


Output :-



hi, Welcome to the Currency Converter!
which currency You want to Convert ? 
1:Ruppe  2:Dollar  3:Pound 
4:Euro  5:Yen  6:Ringgit 
1
How much Money you want to convert ?
1000
Your 1000.0 Rupee is : 14.29 Dollar
Your 1000.0 Rupee is : 11.36 Pound
Your 1000.0 Rupee is : 12.5 Euro
Your 1000.0 Rupee is : 1587.3 Yen
Your 1000.0 Rupee is : 62.5 ringgit
Thank you for choosing our Anonymous Programs




10. Convert Milliseconds to Minutes and Seconds


import java.util.concurrent.TimeUnit;
public class Milliseconds{
    public static void main(String[]args){
        long milliseconds = 15168461;
        long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds);
        System.out.format("%d Milliseconds = %d minutes\n", milliseconds, minutes );
        System.out.println("0r");
        System.out.format("%d Milliseconds = %d seconds", milliseconds, seconds );
    }
}    


Output :-


15168461 Milliseconds = 252 minutes
0r
15168461 Milliseconds = 15168 seconds




Post a Comment

0 Comments