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

1 Answer

0 votes
program TravelTimeCalculator;

var
  distance, speed, tm : real;
  hours, minutes : integer;

begin
  distance := 30; // (in km)
  speed := 90; // (in km/h);

  { Check to avoid division by zero }
  if speed <= 0 then
  begin
    WriteLn('Speed must be greater than zero.');
    Halt(1);
  end;

  { Calculate time }
  tm := distance / speed;

  { Output result }
  WriteLn('Time required: ', tm:0:2, ' hours');

  { Optional: convert to hours and minutes }
  hours := Trunc(tm);
  minutes := Round((tm - hours) * 60);

  WriteLn('Which is approximately ', hours, ' hours and ', minutes, ' minutes.');
end.



(*
run:

Time required: 0.33 hours
Which is approximately 0 hours and 20 minutes.

*)





 



answered Dec 6, 2025 by avibootz

Related questions

...