From 727e3c59346da4f91284b34b4c18f2e0ba155e53 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 9 Aug 2025 16:03:28 +0200 Subject: Initial commit --- hello/hello.gpr | 5 +++ hello/src/main.adb | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 hello/hello.gpr create mode 100644 hello/src/main.adb (limited to 'hello') 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 @@ +project Hello is + for Source_Dirs use ("src"); + for Object_Dir use "obj"; + for Main use ("main.adb"); +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 @@ +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; + +procedure main is + + function Factorial (N : Integer) return Integer is + F : Integer := 1; + begin + for i in 2 .. N loop + F := F * i; + end loop; + return F; + end Factorial; + + function Fib (N : Integer) return Integer is + F : array (0 .. N) of Integer; + begin + F (0) := 0; + F (1) := 1; + for I in F'First + 2 .. F'Last loop + F (I) := F (I - 2) + F (I - 1); + end loop; + return F (N); + end Fib; + + function Fib_Rec (N : Integer) return Integer is + begin + if N = 0 then + return 0; + elsif N = 1 then + return 1; + else + return Fib_Rec (N - 1) + Fib_Rec (N - 2); + end if; + end Fib_Rec; + + procedure Greet_5 is + counter : Integer := 1; + begin + Put_Line ("Greet_5"); + loop + Put_Line ("Counter: " & Integer'Image (counter)); + exit when counter = 5; + counter := counter + 1; + end loop; + end Greet_5; + + procedure Greet_With_While is + counter : Integer := 1; + begin + Put_Line ("Greet_With_While"); + while counter <= 5 loop + Put_Line ("Counter: " & Integer'Image (counter)); + counter := counter + 1; + end loop; + end Greet_With_While; + + procedure Swap (A, B : in out Integer) is + Tmp : Integer; + begin + Tmp := A; + A := B; + B := Tmp; + end Swap; + + procedure Guessing_Game is + Answer : Integer := 47; + Guess : Integer; + begin + loop + Put ("Enter a number: "); + Get (Guess); + if Guess < Answer then + Put_Line ("Too low!"); + elsif Guess > Answer then + Put_Line ("Too high!"); + else + Put_Line ("Correct!"); + exit; + end if; + end loop; + end Guessing_Game; + + N : Integer; + X : Integer := 2; + Y : Integer := 3; + +begin + Put ("Enter an integer value: "); + Get (N); + if N >= 0 then + Put_Line ("Fib(" & Integer'Image (N) & ") = " & Integer'Image (Fib (N))); + Put_Line + ("Factorial(" & Integer'Image (N) & ") = " & + Integer'Image (Factorial (N))); + else + Put_Line ("Please enter a non-negative integer"); + end if; + + Greet_5; + Greet_With_While; + + Put_Line ("Swapping " & Integer'Image (X) & " and " & Integer'Image (Y)); + Swap (X, Y); + Put_Line ("X = " & Integer'Image (X) & ", Y = " & Integer'Image (Y)); + + Guessing_Game; +end main; -- cgit v1.2.3