8. Function Examples and Applications
#include <stdio.h>
// Function prototypes
void display_menu(void);
int get_choice(void);
void calculator_add(void);
void calculator_multiply(void);
void display_table(int num);
int main() {
int choice;
do {
display_menu();
choice = get_choice();
switch (choice) {
case 1:
calculator_add();
break;
case 2:
calculator_multiply();
break;
case 3:
printf("Enter number for multiplication table: ");
int num;
scanf("%d", &num);
display_table(num);
break;
case 4:
printf("Thank you for using the calculator!\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
if (choice != 4) {
printf("\nPress Enter to continue...");
getchar();
getchar(); // Clear buffer
}
} while (choice != 4);
return 0;
}
void display_menu(void) {
printf("\n=== SIMPLE CALCULATOR ===\n");
printf("1. Addition\n");
printf("2. Multiplication\n");
printf("3. Multiplication Table\n");
printf("4. Exit\n");
printf("========================\n");
}
int get_choice(void) {
int choice;
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
return choice;
}
void calculator_add(void) {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Result: %.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
}
void calculator_multiply(void) {
float num1, num2;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
printf("Result: %.2f × %.2f = %.2f\n", num1, num2, num1 * num2);
}
void display_table(int num) {
printf("\nMultiplication table for %d:\n", num);
printf("-------------------------\n");
for (int i = 1; i <= 10; i++) {
printf("%d × %d = %d\n", num, i, num * i);
}
}
8.2 Temperature Conversion Program
#include <stdio.h>
// Function prototypes
float celsius_to_fahrenheit(float celsius);
float fahrenheit_to_celsius(float fahrenheit);
float celsius_to_kelvin(float celsius);
float kelvin_to_celsius(float kelvin);
void display_conversion_table(void);
int main() {
int choice;
float temp, result;
printf("=== TEMPERATURE CONVERTER ===\n");
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("3. Celsius to Kelvin\n");
printf("4. Kelvin to Celsius\n");
printf("5. Display Conversion Table\n");
printf("============================\n");
printf("Enter choice (1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter temperature in Celsius: ");
scanf("%f", &temp);
result = celsius_to_fahrenheit(temp);
printf("%.2f°C = %.2f°F\n", temp, result);
break;
case 2:
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &temp);
result = fahrenheit_to_celsius(temp);
printf("%.2f°F = %.2f°C\n", temp, result);
break;
case 3:
printf("Enter temperature in Celsius: ");
scanf("%f", &temp);
result = celsius_to_kelvin(temp);
printf("%.2f°C = %.2fK\n", temp, result);
break;
case 4:
printf("Enter temperature in Kelvin: ");
scanf("%f", &temp);
if (temp < 0) {
printf("Error: Temperature cannot be below 0 Kelvin!\n");
} else {
result = kelvin_to_celsius(temp);
printf("%.2fK = %.2f°C\n", temp, result);
}
break;
case 5:
display_conversion_table();
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
float celsius_to_fahrenheit(float celsius) {
return (celsius * 9.0 / 5.0) + 32.0;
}
float fahrenheit_to_celsius(float fahrenheit) {
return (fahrenheit - 32.0) * 5.0 / 9.0;
}
float celsius_to_kelvin(float celsius) {
return celsius + 273.15;
}
float kelvin_to_celsius(float kelvin) {
return kelvin - 273.15;
}
void display_conversion_table(void) {
printf("\n=== TEMPERATURE CONVERSION TABLE ===\n");
printf("Celsius | Fahrenheit | Kelvin\n");
printf("---------+------------+--------\n");
for (int c = 0; c <= 100; c += 10) {
float f = celsius_to_fahrenheit(c);
float k = celsius_to_kelvin(c);
printf("%8d | %10.1f | %6.1f\n", c, f, k);
}
}
No comments to display
No comments to display