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 --- basics/src/main.adb | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 basics/src/main.adb (limited to 'basics/src') 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 @@ +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; + +procedure Main is + procedure Read_Number is + N : Integer; + begin + Put_Line ("Please enter a number"); + Get (N); + if N > 0 then + Put_Line ("The number is positive"); + elsif N = 0 then + Put_Line ("The number is zero"); + else + Put_Line ("The number is negative"); + end if; + end Read_Number; + + procedure Test_Loop is + N : Integer := 0; + begin + loop + exit when N = 5; + N := N + 1; + Put_Line ("Test loop"); + end loop; + end Test_Loop; + + procedure Test_Even_Odd is + begin + for I in 0 .. 10 loop + Put_Line (I'Image & " is " & (if I mod 2 = 1 then "Odd" else "Even")); + end loop; + end Test_Even_Odd; + + procedure My_Swap (A : in out Integer; B : in out Integer) is + C : Integer; + begin + C := A; + A := B; + B := C; + end My_Swap; + + procedure Test_My_Swap is + A : Integer := 1; + B : Integer := 3; + begin + Put_Line ("Before swap: " & A'Image & B'Image); + My_Swap (A, B); + Put_Line ("After swap: " & A'Image & B'Image); + end Test_My_Swap; + + function Fib (N : Integer) return Integer is + F0 : Integer := 0; + F1 : Integer := 1; + F : Integer := 0; + begin + for I in 2 .. N loop + F := F0 + F1; + F0 := F1; + F1 := F; + end loop; + return F; + end Fib; + + 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; + + procedure Test_Functions is + N : Integer; + begin + Put_Line ("Enter a number:"); + Get (N); + Put_Line ("Fib(" & N'Image & ") = " & Fib (N)'Image); + Put_Line ("Factorial(" & N'Image & ") = " & Factorial (N)'Image); + end Test_Functions; + + procedure Test_Integers is + type Day is range 1 .. 7; + My_Day : Day := 3; + Other_Day : Day; + begin + for D in Day loop + Put_Line ("Day" & D'Image); + end loop; + Put_Line (My_Day'Image); + Other_Day := My_Day + Day (4); + Put_Line (Other_Day'Image); + end Test_Integers; + +begin + -- This is a comment. + Put_Line ("Hello world!"); + + Test_Loop; + Test_Even_Odd; + Test_My_Swap; + Test_Integers; + + --Read_Number; + Test_Functions; +end Main; -- cgit v1.2.3