ADA Program to print a integer number entered by user
Program
With Gnat.IO; use Gnat.IO;
procedure gnatioint is
I : Integer;
begin
Put ("Enter an integer: ");
Get (I);
Put ("You entered: ");
Put (I);
New_Line;
end;
This Ada program reads an integer input from the user and prints it.
-
With Gnat.IO; use Gnat.IO;- This imports the
Gnat.IOpackage, which provides input/output functionalities. - The
use Gnat.IO;statement allows direct use of functions likePutandGetwithout prefixing them withGnat.IO..
- This imports the
-
procedure gnatioint is- Defines a procedure named
gnatioint, which acts as the main function.
- Defines a procedure named
-
I : Integer;- Declares an integer variable
Ito store the user input.
- Declares an integer variable
-
begin- Marks the start of the execution block.
-
Put ("Enter an integer: ");- Prints the message
"Enter an integer: "to prompt the user.
- Prints the message
-
Get (I);- Reads an integer input from the user and stores it in variable
I.
- Reads an integer input from the user and stores it in variable
-
Put ("You entered: ");- Prints
"You entered: "as part of the output message.
- Prints
-
Put (I);- Prints the value of the entered integer.
-
New_Line;- Moves the cursor to the next line for better formatting.
-
end;- Marks the end of the program.
Key Features:
- Uses
Gnat.IO: A simple input/output library in Ada. - Type-Safe Input Handling: Ensures that only integers are accepted.
- User-Friendly Output: Prompts and displays user input clearly.
Output
$ gnat make gnatioint.adb
gcc -c gnatioint.adb
gnatbind -x gnatioint.ali
gnatlink gnatioint.ali
$ ./gnatioint
Enter an integer: 6
You entered: 6