Learn ADA with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
Ada Counter and Theme Toggle
with Ada.Text_IO; use Ada.Text_IO;
procedure Counter is
Count : Integer := 0;
Is_Dark : Boolean := False;
procedure Update_UI is
begin
Put_Line("Counter: " & Integer'Image(Count));
Put_Line("Theme: " & (if Is_Dark then "Dark" else "Light"));
end Update_UI;
begin
-- Initial display
Update_UI;
-- Increment counter
Count := Count + 1;
Update_UI;
-- Increment counter again
Count := Count + 1;
Update_UI;
-- Toggle theme
Is_Dark := not Is_Dark;
Update_UI;
-- Decrement counter
Count := Count - 1;
Update_UI;
-- Reset counter
Count := 0;
Update_UI;
end Counter;
Demonstrates a simple counter with theme toggling using Ada variables and console output.
2
Ada Simple Addition
with Ada.Text_IO; use Ada.Text_IO;
procedure Add is
A, B, Sum : Integer;
begin
A := 10;
B := 20;
Sum := A + B;
Put_Line("Sum: " & Integer'Image(Sum));
end Add;
Adds two numbers and prints the result.
3
Ada Factorial
with Ada.Text_IO; use Ada.Text_IO;
procedure Factorial is
function Fact(N: Integer) return Integer is
begin
if N <= 1 then
return 1;
else
return N * Fact(N-1);
end if;
end Fact;
begin
Put_Line("Factorial 5: " & Integer'Image(Fact(5)));
end Factorial;
Calculates factorial recursively.
4
Ada Fibonacci Sequence
with Ada.Text_IO; use Ada.Text_IO;
procedure Fibonacci is
function Fib(N: Integer) return Integer is
begin
if N < 2 then
return N;
else
return Fib(N-1) + Fib(N-2);
end if;
end Fib;
begin
for I in 0..9 loop
Put_Line(Integer'Image(Fib(I)));
end loop;
end Fibonacci;
Generates first 10 Fibonacci numbers.
5
Ada Max of Two Numbers
with Ada.Text_IO; use Ada.Text_IO;
procedure Max_Num is
A, B, Max : Integer;
begin
A := 10;
B := 20;
if A > B then
Max := A;
else
Max := B;
end if;
Put_Line("Max: " & Integer'Image(Max));
end Max_Num;
Finds the maximum of two numbers.
6
Ada List Sum
with Ada.Text_IO; use Ada.Text_IO;
procedure List_Sum is
Arr : array(1..5) of Integer := (1,2,3,4,5);
Sum : Integer := 0;
begin
for I in Arr'Range loop
Sum := Sum + Arr(I);
end loop;
Put_Line("Sum: " & Integer'Image(Sum));
end List_Sum;
Sums elements of an array.
7
Ada Even Numbers Filter
with Ada.Text_IO; use Ada.Text_IO;
procedure Even_Filter is
Arr : array(1..5) of Integer := (1,2,3,4,5);
begin
for I in Arr'Range loop
if Arr(I) mod 2 = 0 then
Put_Line(Integer'Image(Arr(I)));
end if;
end loop;
end Even_Filter;
Prints even numbers from an array.
8
Ada Conditional Counter Increment
with Ada.Text_IO; use Ada.Text_IO;
procedure Conditional_Increment is
Count : Integer := 3;
begin
if Count < 5 then
Count := Count + 1;
end if;
Put_Line("Count: " & Integer'Image(Count));
end Conditional_Increment;
Increment counter only if less than 5.
9
Ada Resettable Counter
with Ada.Text_IO; use Ada.Text_IO;
procedure Resettable_Counter is
Count : Integer := 0;
begin
Count := Count + 1;
Count := Count + 1;
Put_Line("Count: " & Integer'Image(Count));
Count := 0;
Put_Line("Count: " & Integer'Image(Count));
end Resettable_Counter;
Counter that increments and can be reset.
10
Ada Theme Toggle Only
with Ada.Text_IO; use Ada.Text_IO;
procedure Theme_Toggle_Only is
Is_Dark : Boolean := False;
begin
Put_Line("Theme: " & (if Is_Dark then "Dark" else "Light"));
Is_Dark := not Is_Dark;
Put_Line("Theme: " & (if Is_Dark then "Dark" else "Light"));
Is_Dark := not Is_Dark;
Put_Line("Theme: " & (if Is_Dark then "Dark" else "Light"));
end Theme_Toggle_Only;
Toggles theme multiple times.