In this section of the java program tutorial, we will learn how to write a Java Currency Converter Program and see how it works. Before we begin, let me tell you that this is a beginner’s tutorial for the currency converter program in java.
Although there are many ways in which you can create a currency converter using java. You can create separate methods or you can use if..else-if statement, here we are going to use a switch case statement for the currency converter.
Java Currency Converter program using Switch case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | import java.util.*; import java.text.DecimalFormat; public class CurrencyConverter { public static void main(String[] args) { double amount; double rupee, dollar, pound, euro, yen, ringgit; int choice; DecimalFormat f = new DecimalFormat("##.##"); Scanner sc = new Scanner(System.in); System.out.println("Following are the Choices:"); System.out.println("Enter 1: Ruppe"); System.out.println("Enter 2: Dollar"); System.out.println("Enter 3: Pound"); System.out.println("Enter 4: Euro"); System.out.println("Enter 5: Yen"); System.out.println("Enter 5: Ringgit"); System.out.print("\nChoose from above options: "); choice = sc.nextInt(); System.out.println("Enter the amount you want to convert?"); amount = sc.nextFloat(); switch (choice) { case 1: // Ruppe Conversion dollar = amount / 70; System.out.println(amount + " Rupee = " + f.format(dollar) + " Dollar"); pound = amount / 88; System.out.println(amount + " Rupee = " + f.format(pound) + " Pound"); euro = amount / 80; System.out.println(amount + " Rupee = " + f.format(euro) + " Euro"); yen = amount / 0.63; System.out.println(amount + " Rupee = " + f.format(yen) + " Yen"); ringgit = amount / 16; System.out.println(amount + " Rupee = " + f.format(ringgit) + " ringgit"); break; case 2: // Dollar Conversion rupee = amount * 70; System.out.println(amount + " Dollar = " + f.format(rupee) + " Ruppes"); pound = amount *0.78; System.out.println(amount + " Dollar = " + f.format(pound) + " Pound"); euro = amount *0.87; System.out.println(amount + " Dollar = " + f.format(euro) + " Euro"); yen = amount *111.087; System.out.println(amount + " Dollar = " + f.format(yen) + " Yen"); ringgit = amount *4.17; System.out.println(amount + " Dollar = " + f.format(ringgit) + " ringgit"); break; case 3: // Pound Conversion rupee = amount * 88; System.out.println(amount + " pound = " + f.format(rupee) + " Ruppes"); dollar = amount *1.26; System.out.println(amount + " pound = " + f.format(dollar) + " Dollar"); euro = amount *1.10; System.out.println(amount + " pound = " + f.format(euro) + " Euro"); yen = amount *140.93; System.out.println(amount + " pound = " + f.format(yen) + " Yen"); ringgit = amount *5.29; System.out.println(amount + " pound = " + f.format(ringgit) + " ringgit"); break; case 4: // Euro Conversion rupee = amount * 80; System.out.println(amount + " euro = " + f.format(rupee) + " Ruppes"); dollar = amount *1.14; System.out.println(amount + " euro = " + f.format(dollar) + " Dollar"); pound = amount *0.90; System.out.println(amount + " euro = " + f.format(pound) + " Pound"); yen = amount *127.32; System.out.println(amount + " euro = " + f.format(yen) + " Yen"); ringgit = amount *4.78; System.out.println(amount + " euro = " + f.format(ringgit) + " ringgit"); break; case 5: // Yen Conversion rupee = amount *0.63; System.out.println(amount + " yen = " + f.format(rupee) + " Ruppes"); dollar = amount *0.008; System.out.println(amount + " yen = " + f.format(dollar) + " Dollar"); pound = amount *0.007; System.out.println(amount + " yen = " + f.format(pound) + " Pound"); euro = amount *0.007; System.out.println(amount + " yen = " + f.format(euro) + " Euro"); ringgit = amount *0.037; System.out.println(amount + " yen = " + f.format(ringgit) + " ringgit"); break; case 6: // Ringgit Conversion rupee = amount *16.8; System.out.println(amount + " ringgit = " + f.format(rupee) + " Ruppes"); dollar = amount *0.239; System.out.println(amount + " ringgit = " + f.format(dollar) + " dollar"); pound = amount *0.188; System.out.println(amount + " ringgit =: " + f.format(pound) + " pound"); euro = amount *0.209; System.out.println(amount + " ringgit = " + f.format(euro) + " euro"); yen = amount *26.63; System.out.println(amount + " ringgit = " + f.format(yen) + " yen"); break; //Default case default: System.out.println("Invalid Input"); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Following are the Choices: Enter 1: Ruppe Enter 2: Dollar Enter 3: Pound Enter 4: Euro Enter 5: Yen Enter 5: Ringgit Choose from above options: 3 Enter the amount you want to convert? 200 200.0 pound = 17600 Ruppes 200.0 pound = 252 Dollar 200.0 pound = 220 Euro 200.0 pound = 28186 Yen 200.0 pound = 1058 ringgit |
The above java program is the simple one as you can see that the conversion is done using the basic switch statement. And as told earlier, you can do the same by using the if..else-if ladder statement, or either you can create a separate function for each currency conversion. This is how you can create a basic currency converter in java program.