#include <stdio.h>
// define the C to F function
float c2f(float c) {
float f = (c * 9/5) + 32;
return f;
}
// define the display function
void displ(float c) {
printf("%3.2f degrees C is %3.2f degrees F\n", c, c2f(c));
}
// Display C to F conversions
int main() {
displ(20);
displ(30);
displ(0);
displ(-40);
}
- Running the above gives:
20.00 degrees C is 68.00 degrees F
30.00 degrees C is 86.00 degrees F
0.00 degrees C is 32.00 degrees F
-40.00 degrees C is -40.00 degrees F
#include <stdio.h>
// define the to MPG function
float mpg(float lp) {
// convert l to imp. gal. (.22) km to miles (1.6)
float m = 1 / (lp/100 * .22 * 1.6);
return m;
}
int main() {
printf("mpg for 4 l/100km is %3.2f mpg\n", mpg(4));
}
mpg for 4 l/100km is 71.02 mpg
#!/usr/bin/env php
<?php
// Define the C to F function
function c2f($c) {
return ($c * 9/5) + 32;
}
// Define the display function
function displ($c) {
echo ("$c degrees C is " . c2f($c) . " degrees F\n");
}
displ(20);
displ(30);
displ(0);
displ(-40);
?>
- Running the above gives:
20 degrees C is 68 degrees F
30 degrees C is 86 degrees F
0 degrees C is 32 degrees F
-40 degrees C is -40 degrees F
#!/usr/bin/env php
<?php
// define the to MPG function
function mpg($lp) {
// convert l to imp. gal. (.22) km to miles (1.6)
$m = 1 / ($lp/100 * .22 * 1.6);
return $m;
}
echo ("mpg for 4 l/100km is " . round(mpg(4),2));
?>
- Running the above gives:
mpg for 4 l/100km is 71.02
Convert litres per 100 km to mpg
Mileage for 14 l/100km is 20.29 mpg.
#!/usr/bin/env python3
def c2f(c):
return (c * 9/5) + 32
def displ(c):
print(str(c) + " degrees C is " + str(c2f(c)) + " degrees F");
displ(20)
displ(30)
displ(0)
displ(-40)
- Running the above gives:
20 degrees C is 68.0 degrees F
30 degrees C is 86.0 degrees F
0 degrees C is 32.0 degrees F
-40 degrees C is -40.0 degrees F
#!/usr/bin/env python
# define the to MPG function
def mpg(lp):
# convert l to imp. gal. (.22) km to miles (1.6)
m = 1 / (lp/100.0 * .22 * 1.6)
return m
print ("mpg for 4 l/100km is " + str(round(mpg(4),2)))
- Running the above gives:
mpg for 4 l/100km is 71.02
#!/usr/bin/env bash
c2f () {
f=$(( $1 * 90 / 5 / 10 + 32 ))
echo "$1 degrees C is $f degrees F";
}
echo $(c2f 20)
echo $(c2f 30)
echo $(c2f 0)
echo $(c2f -40)
- Running the above gives:
20 degrees C is 68 degrees F
30 degrees C is 86 degrees F
0 degrees C is 32 degrees F
-40 degrees C is -40 degrees F
#!/usr/bin/env bash
# We use bc because bash does not do floating point arithmetic.
mpg () {
# Pipe the mpg calculation to bc and save the result in m
# If bc is invoked with the -l option, a math library is preloaded and the
# default scale is set to 20.
m=`echo "1 / ($1 / 100.0 * .22 * 1.6)" | bc -l`;
echo $m
}
printf "mpg is %.2f \n" $(mpg 4);
- Running the above gives:
mpg is 71.02