1

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;
Faibbus
  • 1,115
  • 10
  • 18
Justiciar
  • 356
  • 1
  • 3
  • 20
  • Not quite what you are asking about, but please spend a moment to find out which range of values your program is supposed to be able to factor. - And once you have found out, declare an appropriate type, instead of using `Standard.Integer` whose range is compiler and platform dependent. – Jacob Sparre Andersen Sep 17 '17 at 05:55
  • This is more of an algorithm question than a programming question, and a good exercise to figure out what to do on your own. As someone who assigns homework, I advice you to take your time and only ask here if you have an idea of what to do, but cannot figure out *how* to do it. – flyx Sep 19 '17 at 08:56

0 Answers0