Build a Currency Converter in C Language with Free Codes

Table of Contents

Introduction – Currency Converter

Hello guys, here we are going to create an Currency Converter in C Language. This is Console based Tinny Project which is used for boost switch case skills. Here you get free codes of this Project with Complete Explanation.

Project Features

Here are the features of our Project,

  1. USD to EURO
  2. USD to GBP
  3. USD to JPY

Code::Blocks

For get the output of the Currency Converter Project here we used the  Code::Blocks software. It is a beginner friendly and mostly used software by the C Language Developers.

Free Hand-Written Notes

Output – Currency Converter

Project Explanation

Here we explain the complete project to understand how the program flow works,

1 – File of Header

#include <stdio.h>
#include <stdlib.h>

Here we include the necessary standard header files which is used for Input/Output Functions and Standard Library Functions – Respectively.

2 – Function Definition

// Function to convert amount from one currency to another
float convertCurrency(float amount, float exchangeRate) {
    return amount * exchangeRate;
}

Here we write ‘convertCurrency’ which takes two arguments: First is the amount to convert and the exchange rate. It calculates and returns the equivalent amount in the target currency by multiplying the amount with the exchange rate.

3 – Main Function

int main() {
    // Define exchange rates
    float usdToEuro = 0.83;
    float usdToGBP = 0.72;
    float usdToJPY = 109.75;

    int choice;
    float amount;

    printf("Currency Converter\n");
    printf("1. USD to Euro\n");
    printf("2. USD to GBP\n");
    printf("3. USD to JPY\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

     return 0;
}

In the Main Function we defines the exchange rate for USD to EURO, GBP, and JPY. It Displays the Menu For User to choose the currency conversion:

  • Option 1 : USD to EURO
  • Option 2 : USD to GBP
  • Option 3 : USD to JPY

It reads the user’s choice by using the ‘scanf’. And Based on the user’s choice. It prompts the user to enter the amount in USD. And then calls the ‘convertCurrency’ function with the appropriate exchange rate and displays the equivalent amount in the chosen currency using ‘printf’.

4 – Switch Statement

switch (choice) {
        case 1:
            printf("Enter amount in USD: $");
            scanf("%f", &amount);
            printf("Equivalent amount in Euro: €%.2f\n", convertCurrency(amount, usdToEuro));
            break;
        case 2:
            printf("Enter amount in USD: $");
            scanf("%f", &amount);
            printf("Equivalent amount in GBP: £%.2f\n", convertCurrency(amount, usdToGBP));
            break;
        case 3:
            printf("Enter amount in USD: $");
            scanf("%f", &amount);
            printf("Equivalent amount in JPY: ¥%.2f\n", convertCurrency(amount, usdToJPY));
            break;
        default:
            printf("Invalid choice.\n");
    }

Switch Statement is used for handles user input by executing different code block based on the chosen option:

  • Case 1 : Converts USD to EURO
  • Case 2 : Converts USD to GBP
  • Case 3 : Converts USD to JPY
  • Default : Prints “Invalid Choice” if the user enters an option other than 1,2, or 3.

Complete Codes

#include <stdio.h>
#include <stdlib.h>

// Function to convert amount from one currency to another
float convertCurrency(float amount, float exchangeRate) {
    return amount * exchangeRate;
}

int main() {
    // Define exchange rates
    float usdToEuro = 0.83;
    float usdToGBP = 0.72;
    float usdToJPY = 109.75;

    int choice;
    float amount;

    printf("Currency Converter\n");
    printf("1. USD to Euro\n");
    printf("2. USD to GBP\n");
    printf("3. USD to JPY\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Enter amount in USD: $");
            scanf("%f", &amount);
            printf("Equivalent amount in Euro: €%.2f\n", convertCurrency(amount, usdToEuro));
            break;
        case 2:
            printf("Enter amount in USD: $");
            scanf("%f", &amount);
            printf("Equivalent amount in GBP: £%.2f\n", convertCurrency(amount, usdToGBP));
            break;
        case 3:
            printf("Enter amount in USD: $");
            scanf("%f", &amount);
            printf("Equivalent amount in JPY: ¥%.2f\n", convertCurrency(amount, usdToJPY));
            break;
        default:
            printf("Invalid choice.\n");
    }

    return 0;
}

Conclusion

Here we study about how the Currency Converter is made with C Language, and how the Main Function works. How Switch Case Statement is used properly and efficiently.

Leave a Comment

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

Scroll to Top