My first homework assignment in Ada is to create a program that states whether a number is composite or prime, display its factors, and then indicate its prime factorization. So for example the number 12
would be composite, 1,2,3,4,6,12
. Prime factorization 2 * 2 * 3
.
I'm mostly done, my code indicates if it's a composite number, and all the factors, just not sure how to code the actual prime factorization part where it would display the 2 * 2 * 3
. I have done the rest, but could use some insight as to what code would be the best to proceed.
WITH Ada.Text_IO, Ada.Integer_Text_IO;
USE Ada.Text_IO, Ada.Integer_Text_IO;
PROCEDURE program_one IS
input: File_Type := Ada.Text_IO.Standard_Input;
Value: Integer;
AbsValue: Natural;
factorCount: Integer := 0;
begin
--Open(input, In_File);
WHILE NOT End_Of_File(input) LOOP
IF End_Of_Line(input) THEN
Skip_Line(input);
ELSE
Get(Input, Value);
Put(Value, Width => 1);
absValue := abs Value;
Put(": Positive Factors are: ");
FOR I IN Integer RANGE 1 .. absValue LOOP
IF absValue mod I = 0 THEN
Put(I, Width => 1);
factorCount := factorCount + 1;
IF I /= absValue THEN
Put(", ");
END IF;
END IF;
END LOOP;
New_Line;
IF FactorCount = 2 THEN
Put(Value, 6);
Put(" is prime.");
END IF;
IF FactorCount = 1 OR FactorCount = 0 THEN
Put(Value, 6);
Put(" is neither prime nor composite");
END IF;
IF FactorCount > 2 THEN
Put(Value, 6);
Put(" is composite");
END IF;
factorCount := 0;
New_Line;
END IF;
END LOOP;
Close(Input);
end program_one;