Snippets / c
Unit & Conversions Calculator
cVersatile unit and conversions calculator developed in C, providing accurate and efficient conversions for various measurement units.
#include <stdio.h>
float inchToFeet(float num) { return num / 12; }
float inchToMeter(float num) { return num * 0.0254; }
float centimeterToInch(float num) { return num * 0.393701; }
float kiloToMiles(float num) { return num * 0.621371; }
float poundToKg(float num) { return num * 0.453592; }
float ounceToGram(float num) { return num * 28.3495; }
float gallonToLiter(float num) { return num * 3.78541; }
float fluidOunceToMilliliter(float num) { return num * 29.5735; }
float fahrenheitToCelsius(float num) { return (num - 32) * 5 / 9; }
float celsiusToFahrenheit(float num) { return (num * 9 / 5) + 32; }
float sqFtToSqM(float num) { return num * 0.092903; }
float sqMToSqFt(float num) { return num * 10.7639; }
void lengthCalculator(int case_select, float (*conversionFunction)(float)) {
float input;
printf("To exit enter \"0\" \n");
while (1) {
float result;
switch (case_select) {
case 1:
printf("Enter Inch: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f inch = %f feet \n", input, result);
break;
case 2:
printf("Enter Inch: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f inch = %f meters \n", input, result);
break;
case 3:
printf("Enter Centimeter: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f centimeter = %f inches\n", input, result);
break;
case 4:
printf("Enter Kilometer: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f kilometer = %f miles \n", input, result);
break;
default:
printf("Invalid conversion type! \n");
break;
}
if (input == 0) {
break;
}
}
}
void weightCalculator(int case_select, float (*conversionFunction)(float)) {
float input;
printf("To exit enter \"0\" \n");
while (1) {
float result;
switch (case_select) {
case 1:
printf("Enter Pound: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f pound(lb) = %f kg \n", input, result);
break;
case 2:
printf("Enter Ounce: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f ounce(oz) = %f grams \n", input, result);
break;
default:
printf("Invalid conversion type! \n");
break;
}
if (input == 0) {
break;
}
}
}
void volumeCalculator(int case_select, float (*conversionFunction)(float)) {
float input;
printf("To exit enter \"0\" \n");
while (1) {
float result;
switch (case_select) {
case 1:
printf("Enter Gallon: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f gallon = %f liters \n", input, result);
break;
case 2:
printf("Enter Fluid Ounce: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f Fluid Ounce(fl oz) = %f milliliter(ml) \n", input, result);
break;
default:
printf("Invalid conversion type! \n");
break;
}
if (input == 0) {
break;
}
}
}
void temperatureCalculator(int case_select,
float (*conversionFunction)(float)) {
float input;
printf("To exit enter \"01\" \n");
while (1) {
float result;
switch (case_select) {
case 1:
printf("Enter Fahrenheit: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f F = %f C \n", input, result);
break;
case 2:
printf("Enter Celsius: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f C = %f F \n", input, result);
break;
default:
printf("Invalid conversion type! \n");
break;
}
if (input == 01) {
break;
}
}
}
void areaCalculator(int case_select, float (*conversionFunction)(float)) {
float input;
printf("To exit enter \"0\" \n");
while (1) {
float result;
switch (case_select) {
case 1:
printf("Enter Square Feet: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f sq ft = %f sq m \n", input, result);
break;
case 2:
printf("Enter Square Meter: ");
scanf("%f", &input);
result = (*conversionFunction)(input);
printf("%f sq m = %f sq ft \n", input, result);
break;
default:
printf("Invalid conversion type! \n");
break;
}
if (input == 0) {
break;
}
}
}
int main() {
int choice;
while (1) {
printf("______________Units and Conversions calculator______________\n");
printf("1(Length) | 2(Weight) | 3(Volume) | 4(Temperature) | 5(Area)\n");
printf("____________________________________________________________\n");
printf("Select: ");
scanf("%d", &choice);
if (choice == 1 || choice == 2 || choice == 3 || choice == 4 ||
choice == 5) {
// length calculator
if (choice == 1) {
int select;
while (1) {
printf("*-----------------*\n");
printf(" LENGTH CALCULATOR\n");
printf("*-----------------*\n");
printf("To exit enter \"0\" \n");
printf("1(Inch to Feet) | 2(Inch to Meter) | 3(Centimeter to Inch) | "
"4(Kilometer to Miles)\n");
printf("Select: ");
scanf("%d", &select);
if (select == 0) {
break;
}
switch (select) {
case 1:
printf("________________________________\n");
printf("LENGTH CALCULATOR > Inch to Feet \n");
printf("________________________________\n");
lengthCalculator(1, inchToFeet);
break;
case 2:
printf("_________________________________\n");
printf("LENGTH CALCULATOR > Inch to Meter \n");
printf("_________________________________\n");
lengthCalculator(2, inchToMeter);
break;
case 3:
printf("______________________________________\n");
printf("LENGTH CALCULATOR > Centimeter to Inch \n");
printf("______________________________________\n");
lengthCalculator(3, centimeterToInch);
break;
case 4:
printf("______________________________________\n");
printf("LENGTH CALCULATOR > Kilometer to Miles \n");
printf("______________________________________\n");
lengthCalculator(4, kiloToMiles);
break;
default:
printf("Invalid choice! \n");
break;
}
}
}
// Weight calculator
if (choice == 2) {
int select;
while (1) {
printf("*-----------------*\n");
printf(" WEIGHT CALCULATOR\n");
printf("*-----------------*\n");
printf("To exit enter \"0\" \n");
printf("1(Pound to Kilogram) | 2(Ounce to Gram)\n");
printf("Select: ");
scanf("%d", &select);
if (select == 0) {
break;
}
switch (select) {
case 1:
printf("_____________________________________\n");
printf("WEIGHT CALCULATOR > Pound to Kilogram\n");
printf("_____________________________________\n");
weightCalculator(1, poundToKg);
break;
case 2:
printf("_________________________________\n");
printf("WEIGHT CALCULATOR > Ounce to Gram\n");
printf("_________________________________\n");
weightCalculator(2, ounceToGram);
break;
default:
printf("Invalid choice!\n");
break;
}
}
}
// Volume calculator
if (choice == 3) {
int select;
while (1) {
printf("*-----------------*\n");
printf(" VOLUME CALCULATOR\n");
printf("*-----------------*\n");
printf("To exit enter \"0\" \n");
printf("1(Gallon to Liter) | 2(Fluid Ounce to Milliliter)\n");
printf("Select: ");
scanf("%d", &select);
if (select == 0) {
break;
}
switch (select) {
case 1:
printf("___________________________________\n");
printf("VOLUME CALCULATOR > Gallon to Liter\n");
printf("___________________________________\n");
volumeCalculator(1, gallonToLiter);
break;
case 2:
printf("_____________________________________________\n");
printf("VOLUME CALCULATOR > Fluid Ounce to Milliliter\n");
printf("_____________________________________________\n");
volumeCalculator(2, fluidOunceToMilliliter);
break;
default:
printf("Invalid choice!\n");
break;
}
}
}
// Temperature calculator
if (choice == 4) {
int select;
while (1) {
printf("*----------------------*\n");
printf(" TEMPERATURE CALCULATOR\n");
printf("*----------------------*\n");
printf("To exit enter \"0\" \n");
printf("1(Fahrenheit to Celsius) | 2(Celsius to Fahrenheit)\n");
printf("Select: ");
scanf("%d", &select);
if (select == 0) {
break;
}
switch (select) {
case 1:
printf("___________________________________\n");
printf("VOLUME CALCULATOR > Fahrenheit to Celsius\n");
printf("___________________________________\n");
temperatureCalculator(1, fahrenheitToCelsius);
break;
case 2:
printf("_____________________________________________\n");
printf("VOLUME CALCULATOR > Celsius to Fahrenheit\n");
printf("_____________________________________________\n");
temperatureCalculator(2, celsiusToFahrenheit);
break;
default:
printf("Invalid choice!\n");
break;
}
}
}
// Area calculator
if (choice == 5) {
int select;
while (1) {
printf("*---------------*\n");
printf(" AREA CALCULATOR\n");
printf("*---------------*\n");
printf("To exit enter \"0\" \n");
printf("1(Square Feet to Square Meters) | 2(Square Meter to Square "
"Feet)\n");
printf("Select: ");
scanf("%d", &select);
if (select == 0) {
break;
}
switch (select) {
case 1:
printf("______________________________________________\n");
printf("AREA CALCULATOR > Square Feet to Square Meters\n");
printf("______________________________________________\n");
areaCalculator(1, sqFtToSqM);
break;
case 2:
printf("_____________________________________________\n");
printf("AREA CALCULATOR > Square Meter to Square Feet\n");
printf("_____________________________________________\n");
areaCalculator(2, sqMToSqFt);
break;
default:
printf("Invalid choice!\n");
break;
}
}
}
} else {
printf("Invalid choice! \n");
}
}
return 0;
}
Sep 17, 2024
Show code