Next topic | Ada Home Page | Index

First programs

1. Hello world!

First ADA program is required to display a message "Hello!! This is your computer speaking".

Pseudocode

put (Hello!! This is your computer speaking)

Program


--      Skansholm #2.4.2 pp26-28
--      a simple ada output program

with TEXT_IO;		-- declare the package
use TEXT_IO;

procedure hello is

begin 	-- hello
   PUT_LINE ( "Hello!! This is your computer speaking.");
end hello;

2. Reading and Writing Numbers

Calculate the sum and product of two numbers.

Algorithm


1.      get the two numbers
                1.1     ask for the two numbers
                1.2     get number1
                1.3     get number2

2.      calculate and print the sum
                2.1 print "The sum is "
                2.2 print (number1 + number2)

3.      calculate and print the product
                2.1 print "The product is "
                2.2 print (number1 * number2)

Pseudocode


number1: number
number2: number

put (Give me two whole numbers)
get (number1)
get (number2)

put (The sum of the numbers is )
put (number1 + number2)

put (The product of the numbers is )
put (number1 * number2)

Program


-------------------------------------------------------
--      sum_prod -      sum and product, Skansholm #2.4.2
-------------------------------------------------------

with TEXT_IO;   --      specify packages we depend on
use TEXT_IO;

procedure sum_prod is
    --  declare integer I/O package
    package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
    use  int_io;

    --  declare any constants and variables required
    number1, number2 : integer; --      numbers used

begin --        sum_prod
    --  ask user for numbers and read them
    PUT_LINE("Give me two whole numbers!");
    GET(number1);
    GET(number2);

    --  display sum and product of numbers
    PUT("The sum of the numbers is:");
    PUT(number1+number2);  NEW_LINE;
    PUT("The product of the numbers is:");
    PUT(number1*number2);  NEW_LINE;
end sum_prod;

Execution


Give me two whole numbers!
4
12
The sum of the numbers is:         16
The product of the numbers is:         48

Alternative solutions

more than one way to solve a given problem specification


with TEXT_IO; use TEXT_IO;

procedure sum_prod is
    --  declare integer I/O package
    package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
    use  int_io;
    --  declare any constants and variables required
    number1, number2, total, product : integer;

begin --        sum_prod
    --  ask user for numbers and read them
    PUT ( "Please enter the first number ");
    GET ( number1 ); skip_line;
    PUT ( "Please enter the second number ");
    GET ( number2   ); skip_line;

    --  display sum and product of numbers
    total := number1 + number2;
    product := number1 * number2;
    PUT("The sum of the numbers is:");
    PUT(total, width=>7);  NEW_LINE;
    PUT("The product of the numbers is:");
    PUT(product, width=>3);  NEW_LINE;
end sum_prod;

"Good" programs

meet specification

natural

understandable

efficient

"elegant"


Next topic | Ada Home Page | Index
x-yao@adfa.oz.au -- 14 May 1997