In C, Convert Currency Values

 


#include<stdio.h>
#include<string.h>
void main(){
	char from_text[10];
	char into_text[10];
	float from_value = 0.0f;
	float into_value = 0.0f;
	int from_currency = 0;
	int into_currency = 0;
	printf("Input currency value to convert:  ");
	fflush(stdin);
	scanf("%f", &from_value);
	printf("Enter 1 if converting from Dollars or 2 if converting from Euros\nChoice:  ");
	fflush(stdin);
	scanf("%d", &from_currency);

	switch(from_currency){
		case 1:
			strcpy(from_text, "dollars");
			break;
		case 2: 
			strcpy(from_text, "euros");
			break;
		default:
			printf("Invalid source currency!!\n");
			return;
	}

	printf("Converting to: 1 (Euros)\n");
	printf("Converting to: 2 (Dollars)\n");
	printf("Converting to: 3 (Yen)\n");
	printf("Converting to: 4 (Pounds)\nChoice:  ");
	fflush(stdin);
	scanf("%d", &into_currency);

	into_value = from_value;
	switch(into_currency){
		case 1: 
			strcpy(into_text, "euros");
			if(1 == from_currency){
				into_value *= 0.85f;
			}
			break;
		case 2: 
			strcpy(into_text, "dollars");
			if(2 == from_currency){
				into_value *= 1.18f;
			}
			break;
		case 3: 
			strcpy(into_text, "yen");
			into_value = from_value * 129.47f;
			if(1 == from_currency){
				into_value = from_value * 110.3f;
			}
			break;
		case 4: 
			strcpy(into_text, "pounds");
			into_value = from_value * 0.91f;
			if(1 == from_currency){
				into_value = from_value * 0.78f;
			}
			break;
		default:
			printf("Invalid target currency!!\n");
			return;
	}

	printf("%.2f %s is equal to %.2f %s\n", from_value, from_text, into_value, into_text);
}
Download

Comments

Popular posts from this blog