w3resource

C Exercises: Converts kilometers per hour to miles per hour


4. Kilometers per Hour to Miles per Hour Conversion

Write a C program that converts kilometers per hour to miles per hour.

Pictorial Presentation:

C Input Output: Converts kilometers per hour to miles per hour

Sample Solution:

C Code:

#include <stdio.h>     // Include the standard input/output header file.

float kmph;              /* kilometers per hour */
float miph;              /* miles per hour (to be computed) */
char  line_text[50];      /* a line from the keyboard */

int main()
{
printf("Input kilometers per hour: ");   // Prompt the user to input kilometers per hour.
fgets(line_text, sizeof(line_text), stdin);   // Read a line of input from the user and store it in 'line_text'.
sscanf(line_text, "%f", &kmph);   // Convert the input to a float and store it in 'kmph'.

miph = (kmph * 0.6213712);   // Convert kilometers per hour to miles per hour.
printf("%f miles per hour\n", miph);   // Print the result in miles per hour.

return(0);   // Return 0 to indicate successful execution of the program.
}

Sample Output:

Input kilometers per hour: 15                                                                                 
9.320568 miles per hour

Flowchart:

C Programming Input Output Flowchart: Converts kilometers per hour to miles per hour.

For more Practice: Solve these Related Problems:

  • Write a C program to convert km/hr to miles/hr and vice versa, allowing the user to select the conversion direction.
  • Write a C program to convert an array of speeds from km/hr to miles/hr and display the maximum speed in miles/hr.
  • Write a C program to convert km/hr to miles/hr and round the result to the nearest tenth.
  • Write a C program to perform speed conversion from km/hr to miles/hr with robust error handling for non-numeric inputs.

C Programming Code Editor:



Previous: Write a C program that prints the perimeter of a rectangle to take its height and width as input.
Next: Write a C program that takes hours and minutes as input, and calculates the total number of minutes.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.