UP | HOME

Exercise 1 solutions

Table of Contents


Python

e01.py

tempc = raw_input("enter the temperature in Celsius: ")
tempc = float(tempc)
tempf = ((9.0 / 5.0) * tempc) + 32.0
print "%.1f degrees Celsius is %.1f degrees Fahrenheit" % (tempc, tempf)

MATLAB / Octave

e01.m

tempc = input('enter the temperature in Celsius: ');
tempf = ((9.0 / 5.0) * tempc) + 32.0;
disp([num2str(tempc),' degrees Celsius is ',num2str(tempf),' degrees Fahrenheit']);

R

e01.R

tempc <- readline("enter the temperature in Celsius: ")
tempc <- as.numeric(tempc)
tempf <- ((9 / 5) * tempc) + 32
cat(tempc, "degrees Celsius is", tempf, "degrees Fahrenheit\n")

C

e01.c

// compile with: gcc -o e01 e01.c

#include <stdio.h>

int main(int argc, char *argv[]) {

  printf("enter the temperature in Celsius: ");
  float tempc;
  scanf("%f", &tempc);
  float tempf = ((9.0/5.0) * tempc) + 32.0;
  printf("%.2f Celsius is %.2f degrees Fahrenheit\n", tempc, tempf);

  return 0;
}

Paul Gribble | fall 2014
This work is licensed under a Creative Commons Attribution 4.0 International License
Creative Commons License