forked from jYOTIHARODE/hacktoberfest2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature_conversion
More file actions
90 lines (76 loc) · 2.26 KB
/
temperature_conversion
File metadata and controls
90 lines (76 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// C program to print a Celsius–Fahrenheit
// temperature conversion table //
// F = (C × 9/5) + 32 //
// C = (F − 32) × 5/9 //
#include <stdio.h>
// Function to convert Celsius to Fahrenheit //
float celsiusToFahrenheit(float celsius)
{
return (celsius * 9 / 5) + 32;
}
// Function to convert Fahrenheit to Celsius //
float fahrenheitToCelsius(float fahrenheit)
{
return (fahrenheit - 32) * 5 / 9;
}
// Function to print table header //
void printHeader(char mode)
{
if (mode == 'C')
{
printf("\n-------------------------------------\n");
printf(" Celsius → Fahrenheit Conversion Table\n");
printf("-------------------------------------\n");
printf(" Celsius (°C) Fahrenheit (°F)\n");
printf("-------------------------------------\n");
}
else
{
printf("\n-------------------------------------\n");
printf(" Fahrenheit → Celsius Conversion Table\n");
printf("-------------------------------------\n");
printf(" Fahrenheit (°F) Celsius (°C)\n");
printf("-------------------------------------\n");
}
}
// Function to print conversion table //
void printConversionTable(float start, float end, float step, char mode)
{
printHeader(mode);
if (mode == 'C')
{
for (float c = start; c <= end; c += step)
{
printf(" %6.2f %8.2f\n", c, celsiusToFahrenheit(c));
}
}
else
{
for (float f = start; f <= end; f += step)
{
printf(" %6.2f %8.2f\n", f, fahrenheitToCelsius(f));
}
}
printf("-------------------------------------\n");
}
// Main function //
int main()
{
char mode;
float start, end, step;
printf("Enter mode (C for Celsius to Fahrenheit, F for Fahrenheit to Celsius): ");
scanf(" %c", &mode);
printf("Enter start value: ");
scanf("%f", &start);
printf("Enter end value: ");
scanf("%f", &end);
printf("Enter step value: ");
scanf("%f", &step);
if (mode == 'C' || mode == 'c')
printConversionTable(start, end, step, 'C');
else if (mode == 'F' || mode == 'f')
printConversionTable(start, end, step, 'F');
else
printf("Invalid mode selected.\n");
return 0;
}