w3resource logo


C Programming Exercises

C Exercises: Converts kilometers per hour to miles per hour

Secondary Nav

C Input Output statement: Exercise-4 with Solution

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

Expected Output :

Input kilometers per hour: 15
9.320568 miles per hour

C Code:

#include <stdio.h>

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: "); fgets(line_text, sizeof(line_text), stdin); sscanf(line_text, "%f", &kmph); miph = (kmph * 0.6213712); printf("%f miles per hour\n", miph); return(0); }

Solution

#include <stdio.h>

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: "); fgets(line_text, sizeof(line_text), stdin); sscanf(line_text, "%f", &kmph); miph = (kmph * 0.6213712); printf("%f miles per hour\n", miph); return(0); }

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.