summaryrefslogtreecommitdiff
path: root/basics/src/main.adb
blob: 63339ae41d6bdf0d9962c3559aa5364fc1bcd073 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;