Javatpoint Logo
Javatpoint Logo

Currency Converter Program in Java

In today's globalized world, currency exchange rates play a pivotal role in international finance and trade. As a result, currency converter programs have become indispensable tools for businesses and individuals alike. In this section, we will explore how to create a simple yet effective currency converter program in Java. This program will allow users to convert between different currencies using up-to-date exchange rates.

The Currency Converter Program

Let's start by creating a Java class for our currency converter program. We'll name it CurrencyConverter. In this class, we will define methods to convert between different currencies using exchange rates. To keep things simple, we'll hardcode the exchange rates, but in a real-world application, you would fetch these rates from a reliable source like a financial API.

CurrencyConverter.java

Output:

Welcome to the Currency Converter!
Menu:
1. Convert USD to EUR
2. Convert EUR to USD
3. Quit
Enter your choice: 1
Enter the amount in USD: 100
100.0 USD is equivalent to 85.0 EUR
Menu:11
1. Convert USD to EUR
2. Convert EUR to USD
3. Quit
Enter your choice: 2
Enter the amount in EUR: 50
50.0 EUR is equivalent to 59.0 USD
Menu:
1. Convert USD to EUR
2. Convert EUR to USD
3. Quit
Enter your choice: 3
Thank you for using the Currency Converter!

Explanation of the Code

  1. We import the Scanner class to take user input.
  2. Inside the main method, we display a welcome message and create a menu-driven program using a while loop. Users can choose to convert USD to EUR, EUR to USD, or quit the program.
  3. We define conversion rates (usdToEurRate and eurToUsdRate) as private static variables for simplicity. In a real application, you would fetch these rates from an external source.
  4. We have two methods, convertUSDToEUR and convertEURToUSD, to perform the actual currency conversions.
  5. In each conversion method, we read the user's input, perform the conversion using the hardcoded rates, and display the result.

In this section, we have learned how to create a simple currency converter program in Java. While this program is basic and uses hardcoded exchange rates, you can enhance it by integrating with external APIs to fetch real-time rates. This project is an excellent starting point for anyone looking to develop more complex currency conversion applications or gain experience with Java programming. Remember that Java's versatility and extensive libraries make it a valuable language for a wide range of software development projects.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA