How to get the 10-logarithm of an Integer?

Viewed 160

I want to ask the user to write in an Integer(N) and then I display the 10-logarithm of the Integer he/she entered. I have successfully calculated the 10-logarithm but do not know how to display it like the following ex:

Write in an Integer: 455666
455666 / 10 = 45566
45566 / 10 = 4556
4556 / 10 = 455
455 / 10 = 45
45 / 10 = 4
Answer: 10-logarithm of 455666 is 5.

Here is my code:

with Ada.Text_IO;                    use Ada.Text_IO;
with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
with Ada.Float_Text_IO;              use Ada.Float_Text_IO;

procedure Logarithm is
  X: Integer;
begin
  Put("Write in an Integer: ");
  Get(X);
  for I in 1..X loop
    if 10**I<=X then
      Put(I);
    end if;
  end loop;
end Logarithm;
1 Answers

The following program will calculate the integer logarithm of any value satisfying the Ada subtype Positive to any Positive base.

with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Integer_Logarithm is
   subtype Base_Range is Integer range 2..Integer'Last;
   Count : Natural := 0;
   Base  : Base_Range;
   Num   : Positive;
   Inpt  : Positive;
begin
   Put ("Enter the integer logarithmic base: ");
   Get (Base);
   Put ("Enter the integer: ");
   Get (Inpt);
   Num := Inpt;
   while Num >= Base loop
      Put (Num'Image & " /" & Base'Image & " =");
      Num := Num / Base;
      Put_Line (Num'Image);
      Count := Count + 1;
   end loop;
   Put_Line
     ("The integer logarithm (base" & Base'image & ") of" & Inpt'Image &
      " is" & Count'Image);
end Integer_Logarithm;

When executed using the example above the resulting output is:

Enter the integer logarithmic base: 10
Enter the integer: 455666
 455666 / 10 = 45566
 45566 / 10 = 4556
 4556 / 10 = 455
 455 / 10 = 45
 45 / 10 = 4
The integer logarithm (base 10) of 455666 is 5

When executed for base 2 and a value of 256 the result is

Enter the integer logarithmic base: 2
Enter the integer: 256
 256 / 2 = 128
 128 / 2 = 64
 64 / 2 = 32
 32 / 2 = 16
 16 / 2 = 8
 8 / 2 = 4
 4 / 2 = 2
 2 / 2 = 1
The integer logarithm (base 2) of 256 is 8
Related