Comparison of a simple program in different programming languages


C

C++

FORTRAN
Pascal
BASIC
Ada
#include <stdio.h>   

int main () 

/* PURPOSE: Compute Miles Per Gallon.
 Demonstrate the structure of simple C++ program
 AUTHOR : John Dalbey
 DATE : Feb 2, 1999 */

{
int StartReading;  /* odometer reading at trip beginning */
int EndReading;    /* odometer reading at trip end */
int Distance;      /* Computed distance traveled */
float FuelUsed;    /* fuel used during the trip */
float MilesPerGal; /* fuel efficiency */
    





/* Obtain odometer readings and fuel used from user */

printf ( " Fuel Efficiency Calculator" );
printf ( "\n" );
printf ( "Enter the odometer reading at the start of the trip: ");
scanf ("%d", &StartReading );
printf ( "Enter the reading at the end: ");
scanf ("%d", &EndReading );
printf ( "How much fuel was used (in gallons)? ");
scanf ("%f", &FuelUsed );
  
/* Compute Distance traveled */
Distance = EndReading - StartReading;
     
/* Compute fuel efficiency in miles per gallon  */
MilesPerGal = Distance / FuelUsed;     

/* Display results */
printf ( "\n" );
printf ( "The distance traveled on the trip was: ");
printf ( "%d", Distance);
printf ( "\n" );
printf ( "Fuel efficiency was ");
printf ( "%4.1f", MilesPerGal);
printf ( " miles per gallon.");
printf ( "\n" );

return 0;

}

#include <iostream.h>
#include <iomanip.h>
int main ()

// PURPOSE: Compute Miles Per Gallon.
// Demonstrate the structure of simple C++ program
// AUTHOR : John Dalbey
// DATE : Feb 2, 1999

{
int StartReading; // odometer reading at trip beginning
int EndReading; // odometer reading at trip end
int Distance; // Computed distance traveled
float FuelUsed; // fuel used during the trip
float MilesPerGal; // fuel efficiency


cout.setf(ios::fixed, ios::floatfield); // Set up floating pt.
cout.setf(ios::showpoint); // output format
cout << setprecision(2);

// Obtain odometer readings and fuel used from user

cout << " Fuel Efficiency Calculator";
cout << endl;
cout << "Enter the odometer reading at the start of the trip: ";
cin >> StartReading;
cout << "Enter the reading at the end: ";
cin >> EndReading;
cout << "How much fuel was used (in gallons)? ";
cin >> FuelUsed;

// Compute Distance traveled
Distance = EndReading - StartReading;

// Compute fuel efficiency in miles per gallon
MilesPerGal = Distance / FuelUsed;

// Display results
cout << endl;
cout << "The distance traveled on the trip was: ";
cout << Distance;
cout << endl;
cout << "Fuel efficiency was ";
cout << MilesPerGal;
cout << " miles per gallon.";
cout << endl;

return 0;

}



PROGRAM FuelUse

! PURPOSE: Compute Miles Per Gallon.
! Demonstrate the structure of simple FORTRAN program
! AUTHOR : John Dalbey
! DATE : Feb 2, 1999


INTEGER StartReading ! odometer reading at trip beginning
INTEGER EndReading ! odometer reading at trip end
INTEGER Distance ! Computed distance traveled
REAL FuelUsed ! fuel used during the trip
REAL MilesPerGal ! fuel efficiency






! Obtain odometer readings and fuel used from user

PRINT *, ' Fuel Efficiency Calculator'
PRINT *
PRINT *, 'Enter the odometer reading at the start of the trip:'
READ *, StartReading
PRINT *, 'Enter the reading at the end: '
READ *, EndReading
PRINT *, 'How much fuel was used (in gallons)? '
READ *, FuelUsed

! Compute Distance traveled
Distance = EndReading - StartReading;

! Compute fuel efficiency in miles per gallon
MilesPerGal = Distance / FuelUsed;

! Display results
PRINT *
PRINT *, 'The distance traveled on the trip was: '
PRINT *, Distance
PRINT *
PRINT *, 'Fuel efficiency was '
PRINT *, MilesPerGal
PRINT *, ' miles per gallon.'
PRINT *

END





PROGRAM FuelUse;

{ PURPOSE: Compute Miles Per Gallon. }
{ Demonstrate the structure of simple Pasal program }
{ AUTHOR : John Dalbey }
{ DATE : Feb 2, 1999 }

VAR
StartReading : INTEGER; { odometer reading at trip beginning }
EndReading : INTEGER; { odometer reading at trip end }
Distance : INTEGER; { Computed distance traveled }
FuelUsed : REAL; { fuel used during the trip }
MilesPerGal : REAL; { fuel efficiency }





BEGIN
{ Obtain odometer readings and fuel used from user }

WRITE (' Fuel Efficiency Calculator');
WRITELN;
WRITE ('Enter the odometer reading at the start of the trip: ');
READ (StartReading);
WRITE ('Enter the reading at the end: ');
READ (EndReading);
WRITE ('How much fuel was used (in gallons)? ');
READ (FuelUsed);

{ Compute Distance traveled }
Distance := EndReading - StartReading;

{ Compute fuel efficiency in miles per gallon }
MilesPerGal := Distance / FuelUsed;

{ Display results }
WRITELN;
WRITE ('The distance traveled on the trip was: ');
WRITE (Distance);
WRITELN;
WRITE ('Fuel efficiency was ');
WRITE (MilesPerGal:2:1);
WRITE (' miles per gallon.');
WRITELN;

END.








' PURPOSE: Compute Miles Per Gallon.
' Demonstrate the structure of simple BASIC program
' AUTHOR : John Dalbey
' DATE : Feb 2, 1999


StartReading% = 0 ' odometer reading at trip beginning
EndReading% = 0 ' odometer reading at trip end
Distance% = 0 ' Computed distance traveled
FuelUsed = 0.0 ' fuel used during the trip
MilesPerGal = 0.0 ' fuel efficiency






' Obtain odometer readings and fuel used from user

PRINT " Fuel Efficiency Calculator"
PRINT
PRINT "Enter the odometer reading at the start of the trip";
INPUT StartReading%
PRINT "Enter the reading at the end";
INPUT EndReading%
PRINT "How much fuel was used (in gallons)";
INPUT FuelUsed

' Compute Distance traveled
Distance% = EndReading% - StartReading%

' Compute fuel efficiency in miles per gallon
MilesPerGal = Distance% / FuelUsed

' Display results
PRINT
PRINT "The distance traveled on the trip was: ";
PRINT Distance%
PRINT
PRINT "Fuel efficiency was ";
PRINT Using "##.#"; MilesPerGal;
PRINT " miles per gallon."
PRINT

END
WITH Text_IO; USE Text_IO;

PROCEDURE FuelUse IS

-- PURPOSE: Compute Miles Per Gallon.
-- Demonstrate the structure of simple Ada program
-- AUTHOR : John Dalbey
-- DATE : Feb 2, 1999


StartReading : INTEGER; -- odometer reading at trip beginning
EndReading : INTEGER; -- odometer reading at trip end
Distance : NATURAL; -- Computed distance traveled
FuelUsed : FLOAT; -- fuel used during the trip
MilesPerGal : FLOAT; -- fuel efficiency

PACKAGE My_Int_IO IS NEW INTEGER_IO(Integer);
PACKAGE My_Flt_IO IS NEW FLOAT_IO(Float);
USE My_Int_IO, My_Flt_IO;

BEGIN
-- Obtain odometer readings and fuel used from user

PUT (" Fuel Efficiency Calculator");
NEW_LINE;
PUT ("Enter the odometer reading at the start of the trip: ");
GET (StartReading);
PUT ("Enter the reading at the end: ");
GET (EndReading);
PUT ("How much fuel was used (in gallons)? ");
GET (FuelUsed);

-- Compute Distance traveled
Distance := EndReading - StartReading;

-- Compute fuel efficiency in miles per gallon
MilesPerGal := Float( Distance ) / FuelUsed;

-- Display results
NEW_LINE;
PUT ("The distance traveled on the trip was: ");
PUT (Distance, Width => 1);
NEW_LINE;
PUT ("Fuel efficiency was ");
PUT (MilesPerGal, FORE=>2, AFT=>1, EXP=>0);
PUT (" miles per gallon.");
NEW_LINE;

END FuelUse;