FizzBuzz in Ada

FizzBuzz is a classic programming exercise that is often used as a simple way to filter out candidates who lack basic programming skills. The problem is simple: write a program that prints the numbers from 1 to 100, but for multiples of 3 print “Fizz” instead of the number, for multiples of 5 print “Buzz”, and for multiples of both 3 and 5 print “FizzBuzz”. While it may seem like a trivial exercise, it can actually be quite challenging for beginners. In this article, we’ll take a look at how to implement FizzBuzz in Ada.
Ada is a high-level programming language designed for safety-critical systems. It was originally developed in the 1970s by the U.S. Department of Defense, and has since become a popular language for mission-critical systems in industries such as aerospace, defense, and healthcare. Despite its reputation for safety and reliability, Ada is also a great language for beginners to learn programming concepts, including FizzBuzz.
Here’s an example implementation of FizzBuzz in Ada:
with Ada.Text_IO;
procedure FizzBuzz is
begin
for i in 1..100 loop
if i mod 3 = 0 and i mod 5 = 0 then
Ada.Text_IO.Put_Line("FizzBuzz");
elsif i mod 3 = 0 then
Ada.Text_IO.Put_Line("Fizz");
elsif i mod 5 = 0 then
Ada.Text_IO.Put_Line("Buzz");
else
Ada.Text_IO.Put_Line(Integer'Image(i));
end if;
end loop;
end FizzBuzz;
Let’s break down this code. First, we include the Ada.Text_IO
package, which provides input and output operations. Next, we define a procedure named FizzBuzz
. Inside the procedure, we use a for
loop to iterate from 1 to 100.
For each number in the loop, we check if it is divisible by 3 and 5. If it is, we print “FizzBuzz” to the console using Ada.Text_IO.Put_Line
. If it is only divisible by 3, we print “Fizz”. If it is only divisible by 5, we print “Buzz”. Finally, if the number is not divisible by 3 or 5, we simply print the number itself using Integer'Image
.
One interesting feature of Ada is its strong typing system. Unlike some other languages, Ada requires variables to be explicitly declared with a type. For example, the variable i
in the for
loop is declared as an integer. This can help prevent common programming errors that can arise from type mismatches.
In conclusion, FizzBuzz is a great programming exercise for beginners, and Ada is a powerful language that can help teach programming concepts while also emphasizing safety and reliability. By implementing FizzBuzz in Ada, we can see how this language can be used for simple tasks as well as more complex, mission-critical systems.
Like this content? Join our free slack.cantoncoders.org or checkout an upcoming events.cantoncoders.org