diff options
author | 3gg <3gg@shellblade.net> | 2025-08-09 16:03:28 +0200 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-08-09 16:03:28 +0200 |
commit | 727e3c59346da4f91284b34b4c18f2e0ba155e53 (patch) | |
tree | 807dccd5cba3c6bae2f8d0c9910157e306c6da5b /hello |
Diffstat (limited to 'hello')
-rw-r--r-- | hello/hello.gpr | 5 | ||||
-rw-r--r-- | hello/src/main.adb | 108 |
2 files changed, 113 insertions, 0 deletions
diff --git a/hello/hello.gpr b/hello/hello.gpr new file mode 100644 index 0000000..3f34ae9 --- /dev/null +++ b/hello/hello.gpr | |||
@@ -0,0 +1,5 @@ | |||
1 | project Hello is | ||
2 | for Source_Dirs use ("src"); | ||
3 | for Object_Dir use "obj"; | ||
4 | for Main use ("main.adb"); | ||
5 | end Hello; | ||
diff --git a/hello/src/main.adb b/hello/src/main.adb new file mode 100644 index 0000000..c9cb966 --- /dev/null +++ b/hello/src/main.adb | |||
@@ -0,0 +1,108 @@ | |||
1 | with Ada.Text_IO; use Ada.Text_IO; | ||
2 | with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; | ||
3 | |||
4 | procedure main is | ||
5 | |||
6 | function Factorial (N : Integer) return Integer is | ||
7 | F : Integer := 1; | ||
8 | begin | ||
9 | for i in 2 .. N loop | ||
10 | F := F * i; | ||
11 | end loop; | ||
12 | return F; | ||
13 | end Factorial; | ||
14 | |||
15 | function Fib (N : Integer) return Integer is | ||
16 | F : array (0 .. N) of Integer; | ||
17 | begin | ||
18 | F (0) := 0; | ||
19 | F (1) := 1; | ||
20 | for I in F'First + 2 .. F'Last loop | ||
21 | F (I) := F (I - 2) + F (I - 1); | ||
22 | end loop; | ||
23 | return F (N); | ||
24 | end Fib; | ||
25 | |||
26 | function Fib_Rec (N : Integer) return Integer is | ||
27 | begin | ||
28 | if N = 0 then | ||
29 | return 0; | ||
30 | elsif N = 1 then | ||
31 | return 1; | ||
32 | else | ||
33 | return Fib_Rec (N - 1) + Fib_Rec (N - 2); | ||
34 | end if; | ||
35 | end Fib_Rec; | ||
36 | |||
37 | procedure Greet_5 is | ||
38 | counter : Integer := 1; | ||
39 | begin | ||
40 | Put_Line ("Greet_5"); | ||
41 | loop | ||
42 | Put_Line ("Counter: " & Integer'Image (counter)); | ||
43 | exit when counter = 5; | ||
44 | counter := counter + 1; | ||
45 | end loop; | ||
46 | end Greet_5; | ||
47 | |||
48 | procedure Greet_With_While is | ||
49 | counter : Integer := 1; | ||
50 | begin | ||
51 | Put_Line ("Greet_With_While"); | ||
52 | while counter <= 5 loop | ||
53 | Put_Line ("Counter: " & Integer'Image (counter)); | ||
54 | counter := counter + 1; | ||
55 | end loop; | ||
56 | end Greet_With_While; | ||
57 | |||
58 | procedure Swap (A, B : in out Integer) is | ||
59 | Tmp : Integer; | ||
60 | begin | ||
61 | Tmp := A; | ||
62 | A := B; | ||
63 | B := Tmp; | ||
64 | end Swap; | ||
65 | |||
66 | procedure Guessing_Game is | ||
67 | Answer : Integer := 47; | ||
68 | Guess : Integer; | ||
69 | begin | ||
70 | loop | ||
71 | Put ("Enter a number: "); | ||
72 | Get (Guess); | ||
73 | if Guess < Answer then | ||
74 | Put_Line ("Too low!"); | ||
75 | elsif Guess > Answer then | ||
76 | Put_Line ("Too high!"); | ||
77 | else | ||
78 | Put_Line ("Correct!"); | ||
79 | exit; | ||
80 | end if; | ||
81 | end loop; | ||
82 | end Guessing_Game; | ||
83 | |||
84 | N : Integer; | ||
85 | X : Integer := 2; | ||
86 | Y : Integer := 3; | ||
87 | |||
88 | begin | ||
89 | Put ("Enter an integer value: "); | ||
90 | Get (N); | ||
91 | if N >= 0 then | ||
92 | Put_Line ("Fib(" & Integer'Image (N) & ") = " & Integer'Image (Fib (N))); | ||
93 | Put_Line | ||
94 | ("Factorial(" & Integer'Image (N) & ") = " & | ||
95 | Integer'Image (Factorial (N))); | ||
96 | else | ||
97 | Put_Line ("Please enter a non-negative integer"); | ||
98 | end if; | ||
99 | |||
100 | Greet_5; | ||
101 | Greet_With_While; | ||
102 | |||
103 | Put_Line ("Swapping " & Integer'Image (X) & " and " & Integer'Image (Y)); | ||
104 | Swap (X, Y); | ||
105 | Put_Line ("X = " & Integer'Image (X) & ", Y = " & Integer'Image (Y)); | ||
106 | |||
107 | Guessing_Game; | ||
108 | end main; | ||