Convert temperatures : Scanf « Console « C / ANSI-C






Convert temperatures



#include <stdio.h>

int main(){
  int choice = 0;
  double temperature = 0.0;
  printf("Select the conversion (1 or 2): ");
  scanf("%d", &choice);

  printf("Enter a temperature in degrees %s: ",
                          (choice == 1 ?  "Centigrade" : "Fahrenheit"));
  scanf("%lf", &temperature);

  if(choice == 1)
    printf("That is equivalent to %.2lf degrees Fahrenheit\n", temperature*9.0/5.0+32.0);
  else
    printf("That is equivalent to %.2lf degrees Centigrade\n", (temperature-32.0)*5.0/9.0);
}


           
       








Related examples in the same category

1.Reading characters with scanf()
2.Pointer argument to scanf
3.Various read: string, float: scanf
4. Read formatted data from string