C Language | Peso-Dollar Conversion

Saturday 8 December 2012


Most Filipinos have relatives who migrated abroad especially in the US. Most of them migrated because they married foreigners, others migrated because of petitions and there are others who migrated because of work. Being a relative living abroad, there are instances that you need to help your relatives back in your hometown and that means sending money. People receiving money from their relatives living in the other countries, especially first timers, tend to confuse the value of the money they receive. US Dollars is one of the currency holding a high currency exchange rate value in the Philippines. That means one (1) US Dollar can be converted to atleast forty (40) Php. But there are instances that the value of US Dollars change due to economic changes as well.

To prevent confusions and to help first timers understand the difference of the two currencies, I made a program that will help people convert currencies easily depending on how much the value of US Dollars are in the Philippines. This program is used with three functions namely main, display and convert. Main function will hold the main program and the other functions hold the conversion and the currency being converted. Check out the codes below.


   1:   
   2:  #include<stdio.h>
   3:  #include<conio.h>
   4:   
   5:  float convert(float pesoValue, int choice, float amount);
   6:  void display(int choice, float result);
   7:   
   8:  int main()
   9:  {
  10:      int choice;
  11:      float pesoValue, amount, result = 0.0;
  12:   
  13:          printf("USD 1 is equivalent to Php ");
  14:          scanf("%f", &pesoValue);
  15:          printf("\nPress 1 for PESO to DOLLAR conversion\nPress 2 for DOLLAR to PESO conversion\n");
  16:       

  17:              while(1){
  18:                  printf("\nChoice: ");
  19:                  scanf("%d", &choice);
  20:                      if( choice == 0)
  21:                          return 0;
  22:                  printf("Amount: ");
  23:                  scanf("%f", &amount);
  24:                  result = convert(pesoValue, choice, amount);
  25:                  display(choice, result);
  26:              }
  27:           
  28:      getch();
  29:  }
  30:   
  31:  float convert(float pesoValue, int choice, float amount)
  32:  {
  33:      float result = 0.0;
  34:   
  35:          switch(choice){
  36:   
  37:              case 1:
  38:                  result = amount / pesoValue;
  39:                  break;
  40:              case 2:
  41:                  result = amount * pesoValue;
  42:                  break;
  43:              default:
  44:                  break;
  45:          }
  46:       
  47:      return result;
  48:  }
  49:   
  50:  void display(int choice, float result)
  51:  {
  52:      switch(choice){
  53:       
  54:          case 1:
  55:              printf(">USD %.2f\n", result); break;
  56:          case 2:
  57:              printf(">Php %.2f\n", result); break;
  58:          default:
  59:              printf(">Invalid Input\n");
  60:      }
  61:  }
  62:   
  63:  


The program operates as follows. The user will be responsible in entering the right value/amount of one (1) US Dollar to Philippine Peso. This part should be filled correctly to allow the program to give the user exact answers. The input will be stored in a memory namely pesoValue. This will act as your initial value to be divided or multiplied with your entered amount. The next step will be choosing what to convert, if it is USD to Php or vice versa. After that, it will ask for an amount then the main function now will call on function float convert to do the processing. Your choice will then go through the switch case statements, the one responsible in handling decision making, to look for the specific case in which your choice will fit.  Then it will return the result in the main function. Main will also call on the function void display to display the result. After that, you can choose whether to continue with another conversion or terminate the program. You can check out the sample output below.




Hope you learned something. We hope to hear from you. Thanks!

1 comment:

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...