summaryrefslogtreecommitdiff
path: root/basics/src/main.adb
diff options
context:
space:
mode:
Diffstat (limited to 'basics/src/main.adb')
-rw-r--r--basics/src/main.adb108
1 files changed, 108 insertions, 0 deletions
diff --git a/basics/src/main.adb b/basics/src/main.adb
new file mode 100644
index 0000000..63339ae
--- /dev/null
+++ b/basics/src/main.adb
@@ -0,0 +1,108 @@
1with Ada.Text_IO; use Ada.Text_IO;
2with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
3
4procedure Main is
5 procedure Read_Number is
6 N : Integer;
7 begin
8 Put_Line ("Please enter a number");
9 Get (N);
10 if N > 0 then
11 Put_Line ("The number is positive");
12 elsif N = 0 then
13 Put_Line ("The number is zero");
14 else
15 Put_Line ("The number is negative");
16 end if;
17 end Read_Number;
18
19 procedure Test_Loop is
20 N : Integer := 0;
21 begin
22 loop
23 exit when N = 5;
24 N := N + 1;
25 Put_Line ("Test loop");
26 end loop;
27 end Test_Loop;
28
29 procedure Test_Even_Odd is
30 begin
31 for I in 0 .. 10 loop
32 Put_Line (I'Image & " is " & (if I mod 2 = 1 then "Odd" else "Even"));
33 end loop;
34 end Test_Even_Odd;
35
36 procedure My_Swap (A : in out Integer; B : in out Integer) is
37 C : Integer;
38 begin
39 C := A;
40 A := B;
41 B := C;
42 end My_Swap;
43
44 procedure Test_My_Swap is
45 A : Integer := 1;
46 B : Integer := 3;
47 begin
48 Put_Line ("Before swap: " & A'Image & B'Image);
49 My_Swap (A, B);
50 Put_Line ("After swap: " & A'Image & B'Image);
51 end Test_My_Swap;
52
53 function Fib (N : Integer) return Integer is
54 F0 : Integer := 0;
55 F1 : Integer := 1;
56 F : Integer := 0;
57 begin
58 for I in 2 .. N loop
59 F := F0 + F1;
60 F0 := F1;
61 F1 := F;
62 end loop;
63 return F;
64 end Fib;
65
66 function Factorial (N : Integer) return Integer is
67 F : Integer := 1;
68 begin
69 for I in 2 .. N loop
70 F := F * I;
71 end loop;
72 return F;
73 end Factorial;
74
75 procedure Test_Functions is
76 N : Integer;
77 begin
78 Put_Line ("Enter a number:");
79 Get (N);
80 Put_Line ("Fib(" & N'Image & ") = " & Fib (N)'Image);
81 Put_Line ("Factorial(" & N'Image & ") = " & Factorial (N)'Image);
82 end Test_Functions;
83
84 procedure Test_Integers is
85 type Day is range 1 .. 7;
86 My_Day : Day := 3;
87 Other_Day : Day;
88 begin
89 for D in Day loop
90 Put_Line ("Day" & D'Image);
91 end loop;
92 Put_Line (My_Day'Image);
93 Other_Day := My_Day + Day (4);
94 Put_Line (Other_Day'Image);
95 end Test_Integers;
96
97begin
98 -- This is a comment.
99 Put_Line ("Hello world!");
100
101 Test_Loop;
102 Test_Even_Odd;
103 Test_My_Swap;
104 Test_Integers;
105
106 --Read_Number;
107 Test_Functions;
108end Main;