Skip to content

Instantly share code, notes, and snippets.

@isaacssemugenyi
Last active April 16, 2023 07:37
Show Gist options
  • Save isaacssemugenyi/d32645854576cc671e79d74c0bef36af to your computer and use it in GitHub Desktop.
Save isaacssemugenyi/d32645854576cc671e79d74c0bef36af to your computer and use it in GitHub Desktop.
/** Currency converter source -> https://www.oanda.com/currency-converter
* 13th/April/2023
* KES -> UGX 1 -> 27.5834
* UGX -> KES 1 -> 0.03533
*
* @author 1. Asiimwe Priscah elvin - 22/2/314/DJ/038
* 2. Isaac Ssemugenyi - 22/2/306/WJ/164
*
* @title KUUK Currency Converter
* @description KUUK - Kenya->Uganda, Uganda->Kenya Currency Converter
* @description Currency converter from Ugx to Ksh or Ksh to Ugx
* */
#include<stdio.h>
// constants for exchange rates
#define UGX_TO_KES 0.03533
#define KES_TO_UGX 27.5834
// Function prototypes
float kenya_to_uganda_shilling(float amount);
float uganda_to_kenya_shilling(float amount);
void switch_currency_type(float amount, char currency_type);
/**
* @function main
* @description entry file of the application
*/
int main()
{
// variables
float amount;
char currency_type;
char want_next_conversion;
// Messages to user and inputs from console
printf("Please enter the amount of money you need to converter in Ugx or Ksh:");
scanf("%f",&amount);
// Prevent scanf to jump to the printf before loading the data into the buffer
while (getchar() != '\n')
{
continue;
}
printf("Type U to convert from Kshs to Ugx, OR K to convert from Ugx to Kshs:");
scanf("%c",&currency_type);
// Invoke switch_currency_type
switch_currency_type(amount, currency_type);
while (getchar() != '\n')
{
continue;
}
printf("\nType Y to make another conversion or N to cancel:");
scanf("%c",&want_next_conversion);
// Check to repeat the process or end program execution
while(want_next_conversion == 'Y'){
main();
want_next_conversion = 'N';
}
printf("\nConverter exited");
return 0;
}
/**
* @function kenya_to_uganda_shilling
* @params {float amount}
* @description function to convert from kenya shillings to uganda shillings
* @example kenya_to_uganda_shilling(500.00)
* @return 13791.7
*/
float kenya_to_uganda_shilling(float amount)
{
return amount * KES_TO_UGX;
}
/**
* @function uganda_to_kenya_shilling
* @params {float amount}
* @description function to convert from uganda shillings to kenya shillings
* @example uganda_to_kenya_shilling(50000.00)
* @return 1766.5
*/
float uganda_to_kenya_shilling(float amount)
{
return amount * UGX_TO_KES;
}
/**
* @function switch_currency_type
* @params {float amount, char currency_type}
* @description function to switch between the respective currency converter
* @example switch_currency_type(50000.00, 'U')
* @description if amount and U is given as parameters, amount will be converted to
* Ugx, if K is given amount will be converted to Ksh and output will be shown on screen.
* if a different value is given for currency_type 'Invalid currency type supplied' will be
* printed
*/
void switch_currency_type(float amount, char currency_type)
{
float output;
switch(currency_type){
case 'U':
output = kenya_to_uganda_shilling(amount);
printf("Converted from KES: %.4f to UGX %.2f", amount, output);
break;
case 'K':
output = uganda_to_kenya_shilling(amount);
printf("Converted from UGX: %.2f to KES %.5f", amount, output);
break;
default:
printf("Invalid currency type supplied\n");
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment