Introduction:

Currency conversion is an essential task when dealing with international financial transactions. The need for converting different currencies arises in various fields such as finance, economics, and travel. This program provides a simple tool to convert amounts between different currencies using predefined exchange rates. The program allows the user to input an amount in one currency and choose the desired target currency for conversion.

Objective:

The objective of this program is to create a simple currency converter in C that allows users to convert an amount from one currency to another based on predefined exchange rates. The program will take user input for the source currency, target currency, and amount to be converted, then output the converted amount.

Currency Converter Program in C

#include 

int main() {
    float amount, convertedAmount;
    int choice;
    const float USD_TO_EUR = 0.85, USD_TO_GBP = 0.75, EUR_TO_USD = 1.18, EUR_TO_GBP = 0.88, GBP_TO_USD = 1.33, GBP_TO_EUR = 1.14;

    printf("Welcome to the Currency Converter\n");
    printf("Choose the currency conversion you want:\n");
    printf("1. USD to EUR\n");
    printf("2. USD to GBP\n");
    printf("3. EUR to USD\n");
    printf("4. EUR to GBP\n");
    printf("5. GBP to USD\n");
    printf("6. GBP to EUR\n");
    printf("Enter your choice (1-6): ");
    scanf("%d", &choice);

    if (choice < 1 || choice > 6) {
        printf("Invalid choice!\n");
        return 1;
    }

    printf("Enter the amount you want to convert: ");
    scanf("%f", &amount);

    switch(choice) {
        case 1:
            convertedAmount = amount * USD_TO_EUR;
            printf("%.2f USD is equal to %.2f EUR\n", amount, convertedAmount);
            break;
        case 2:
            convertedAmount = amount * USD_TO_GBP;
            printf("%.2f USD is equal to %.2f GBP\n", amount, convertedAmount);
            break;
        case 3:
            convertedAmount = amount * EUR_TO_USD;
            printf("%.2f EUR is equal to %.2f USD\n", amount, convertedAmount);
            break;
        case 4:
            convertedAmount = amount * EUR_TO_GBP;
            printf("%.2f EUR is equal to %.2f GBP\n", amount, convertedAmount);
            break;
        case 5:
            convertedAmount = amount * GBP_TO_USD;
            printf("%.2f GBP is equal to %.2f USD\n", amount, convertedAmount);
            break;
        case 6:
            convertedAmount = amount * GBP_TO_EUR;
            printf("%.2f GBP is equal to %.2f EUR\n", amount, convertedAmount);
            break;
    }

    return 0;
}

Program Explanation:

1. Includes and Variable Declarations:

The program begins by including the stdio.h library for input/output operations. It then declares several float variables to store the amount entered by the user and the converted amount. It also declares a choice variable to store the user’s menu selection. The program also defines conversion rates for six common currencies: USD, EUR, and GBP.

2. User Input:

The program displays a menu asking the user to choose the type of currency conversion (USD to EUR, USD to GBP, etc.). It then accepts the user’s choice and the amount they wish to convert. If the user enters an invalid choice (outside the range 1-6), the program will print an error message and terminate.

3. Conversion Logic:

Based on the user’s choice, the program calculates the converted amount using the corresponding conversion rate and displays the result. The conversion rates are defined as constants at the beginning of the program. Each conversion is handled inside a switch statement that matches the user’s choice.

4. Running the Program:

To run the program, follow these steps:

  • Open a C compiler (such as Code::Blocks, DevC++, or an online compiler).
  • Copy and paste the provided code into the compiler.
  • Compile and run the program.
  • Input the conversion type and amount when prompted.
  • The program will display the converted amount on the screen.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)