0% found this document useful (0 votes)
6 views

03_SampleC_Program

The document provides a C program that calculates the time in minutes for a car to travel a given distance in meters at a specified speed in miles per hour. It prompts the user for input, converts the distance to miles, calculates the time in hours, and then converts that time to minutes for output. Two versions of the program are included, both achieving the same result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

03_SampleC_Program

The document provides a C program that calculates the time in minutes for a car to travel a given distance in meters at a specified speed in miles per hour. It prompts the user for input, converts the distance to miles, calculates the time in hours, and then converts that time to minutes for output. Two versions of the program are included, both achieving the same result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

/*

CTIS 151 Introduction to Programming

Purpose: Given a distance in meters and the speed of a car in miles per
hour (mph), calculate how many minutes it will take for that car to
travel that distance.

Written by Berrin Kafkas Feb. 8, 2008


*/

#include <stdio.h>
#define METERS_PER_MILE 1609.344

int main (void)


{
double meters, // input - distance in meters
miles, // distance in miles
speed, // input - speed in mph
hours, // time in hours
minutes; // output - time in minutes

// Get the distance in meters


printf ("Enter the distance in meters: ");
scanf ("%lf", &meters);

// Get the speed of the car in miles per hour


printf ("Enter the speed of the car in mph: ");
scanf ("%lf", &speed);

// Calculate the time in minutes


// Convert the distance in meters to miles
miles = meters / METERS_PER_MILE;
hours = miles / speed; // Calculate the time in hours
minutes = hours * 60; // Convert the time in hours to minutes

// Display the time in minutes


printf ("The car will travel %0.2f meters in %0.1f minutes.\n",
meters, minutes);

return (0);
}

#include <stdio.h>
int main (void)
{double x, y, s, h, m;
printf ("Enter the distance in meters: "); scanf ("%lf", &x);
printf ("Enter the speed of the car in mph: "); scanf ("%lf", &s);
y = x / 1609.344; h = y / s; m = h * 60;
printf ("The car will travel %0.2f meters in %0.1f minutes.\n", x, m);
return (0);}

You might also like