-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmsfunction.c
102 lines (82 loc) · 2.18 KB
/
kmsfunction.c
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
91
92
93
94
95
96
97
98
99
100
101
/*
*
* This program contains the functions for KMS
*
*/
#include<stdio.h>
//function prototype
//asks and gets the choice (1 kilometers to miles or 2 miles to kilometers)
void ConversionFunction();
//asks and get the kilometers
//returns the miles
double KmsToMiles( );
//asks and gets the miles
//returns the kilometers
double MilesToKms( );
int main()
{
//declare the variables to store the data
char again = 'y';
printf("Welcome to the kilometer/ mile calculator. ");
while (again == 'y' || again == 'y') //upper or lowercase y
{
//function call
ConversionFunction();
printf("\nWould you like to do another conversion? (y or n): ");
scanf(" %c", &again);
}
printf("\nHave a great day!\n");
return 0;
}
//asks and gets the kilometers
//returns the miles
double kmstomiles()
{
double kilometers, miles;
//ask and get distance in kilometers
printf("\nEnter distance in kilometers> ");
scanf("%lf", &kilometers);
//convert miles to kilometers
miles = kilometers * 0.62137
return miles;
}
//asks and gets the miles
//returns the kilometers
double milestokms()
{
double miles, kilometers;
//asks and gets distance in miles
printf("\nEnter distance in miles> ");
scanf("%lf", &miles);
//convert miles to kilometers
kilometers = 1.609 * miles;
return kilometers;
}
//implementation
void ConversionFunction()
{
//declare variables to store the data
double miles = 0.0, kilometers = 0.0;
int userChoice;
printf("\n---Enter 1 to convert kilometers to miles or ");
printf("\n---Enter 2 to convert miles to kilometers : ");
scanf("%d", &userChoice);
if (userChoice == 1)
{
//function call
miles = KmsToMiles();
//print the results to the screen
printf("\nThe kilometers you entered would be %.3f miles\n", miles);
}
else if (userChoice == 2)
{
//function call
kilometers = MilesToKms();
//print the results to the screen
printf("\nThe miles you entered would be %.3f kilometers\n"), kilometers);
}
else
{
printf("\nYou did not enter a 1 or a 2\n");
}
}