In this post, we will learn Currency Calculator Program in java Programming language.
Question:
Write a program to convert dollars into Indian money.
The currency calculator is capable of converting only USA, Canada, Singapore and Hong Kong dollars to INR.
Prompt the user to enter the Currency code and the amount to be converted. Display the result as given in the sample output.
Note the currency code and exchange rate for the countries
{Canada –CAD, 52.08} , {Hong Kong – HKD, 8.86}, {Singapore – SGD, 51.29} {USA – USD, 69.55}
Store these values as double and after calculation, print the values to two decimal places. Use System.out.printf to print correct to 2 decimal places.
Sample input 1
Enter the currency code
HKD
Enter the amount
3400
Sample output 1
Hong Kong dollar 3400 equals to 30124.00 Indian Rupee
Sample input 2
AED
Sample output 2
Currency code not found
CODE:–
import java.util.*; import java.text.DecimalFormat; public class Main { public static void main (String[] args) { String country_code[]={"CAD", "HKD", "SGD", "USD"}; String country_name[]={"Canada", "Hong Kong", "Singapore", "USA"}; double exg_rate[]={52.08, 8.86, 51.29, 69.55}; int flag=0; Scanner sc= new Scanner(System.in); System.out.println("Enter the currency code"); String cur=sc.nextLine().toUpperCase(); for(int i=0;i<=3;i++) { if(cur.equals(country_code[i])) { System.out.println("Enter the amount"); int amt=sc.nextInt(); double ans=amt*exg_rate[i]; DecimalFormat f= new DecimalFormat("########0.00"); System.out.println(country_name[i]+" dollar "+amt+" equals to "+f.format(ans)+" Indian Rupee"); flag++; break; } } if(flag==0) { System.out.println("Currency code not found"); } } }