Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,883 answers

573 users

How to calculate time if we have the distance and speed (kilometers per hour) in C

1 Answer

0 votes
#include <stdio.h>

int main() {
    double distance, speed;

    // Input distance and speed
    printf("Enter distance (in km): ");
    scanf("%lf", &distance);

    printf("Enter speed (in km/h): ");
    scanf("%lf", &speed);

    // Check to avoid division by zero
    if (speed <= 0) {
        printf("Speed must be greater than zero.\n");
        return 1;
    }

    // Calculate time
    double tm = distance / speed;

    // Output result
    printf("Time required: %.2f hours\n", tm);

    // Optional: convert to hours and minutes
    int hours = (int)tm;
    int minutes = (int)((tm - hours) * 60);
    printf("Which is approximately %d hours and %d minutes.\n", hours, minutes);

    return 0;
}



/*
run:

Enter distance (in km): 30
Enter speed (in km/h): 90
Time required: 0.33 hours
Which is approximately 0 hours and 20 minutes.

*/


answered Sep 16, 2014 by avibootz
edited Dec 5, 2025 by avibootz

Related questions

...