Hello Experts, I just wanna know how to display prime numbers in TListBox, If I put 10 in EditBox then it would display all the possible numbers fom 1 to 10,and will display to its according listbox.The even numbers in even_listbox, if prime it will display in prime_listbox and if it's odd then it will diplay in odd_Listbox.

Now I already got the even and odd numbers and displayed it to its according listboxes, however my problem is that I dont know how to display prime Numbers to its intended listbox.
Can you help me out of my problem? pls help me...
Your help is very much appreciated, thank you

Here's my current code by the way, (this is Delphi Programming Language and I'm using Lazarus version 1.2.4 as my IDE)

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
        begin
        if (Edit1.Text='') then
    ShowMessage('Input Field Required!')

    else
     for i:=1 to StrToInt(Edit1.Text) do
     begin
      if Odd(i) then
      ListBox3.Items.Add(IntToStr(i))
    else if (i mod 2)=0 then
    ListBox2.Items.Add(IntToStr(i))
    Button1.Enabled:=false;
   Button2.Enabled:=true;

   end;
end;

ListBox1 is for Prime
ListBox2 is for Even and
ListBox3 is for Odd
Button1 is "View"
Edit1.Text is an EditBox wherein, I input a particular number to be displayed in it's according Listboxes.

Dani AI

Generated

Good start, — and is right to flag the odd/even test ordering. The easiest fix is to test for primality first (so 2 appears only in the Prime list), then fall back to odd/even. Also validate the Edit value, clear the listboxes before filling, and move button state changes outside the loop so they are not executed repeatedly.

function IsPrime(n: Integer): Boolean;
var
  i: Integer;
begin
  if n <= 1 then
  begin
    Result := False; Exit;
  end;
  if n = 2 then
  begin
    Result := True; Exit;
  end;
  if (n mod 2) = 0 then
  begin
    Result := False; Exit;
  end;
  i := 3;
  while i * i <= n do
  begin
    if (n mod i) = 0 then
    begin
      Result := False; Exit;
    end;
    i := i + 2;
  end;
  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i, maxN: Integer;
begin
  if not TryStrToInt(Edit1.Text, maxN) or (maxN < 1) then
    Exit; // validate input

  ListBox1.Items.BeginUpdate;
  ListBox2.Items.BeginUpdate;
  ListBox3.Items.BeginUpdate;
  try
    ListBox1.Items.Clear; // prime
    ListBox2.Items.Clear; // even
    ListBox3.Items.Clear; // odd

    for i := 1 to maxN do
    begin
      if IsPrime(i) then
        ListBox1.Items.Add(IntToStr(i))
      else if Odd(i) then
        ListBox3.Items.Add(IntToStr(i))
      else
        ListBox2.Items.Add(IntToStr(i));
    end;
  finally
    ListBox1.Items.EndUpdate;
    ListBox2.Items.EndUpdate;
    ListBox3.Items.EndUpdate;
  end;

  Button1.Enabled := False;
  Button2.Enabled := True;
end;

Notes and troubleshooting: 1 is not prime (so the code above puts it in OddListBox). For very large upper bounds use a Sieve of Eratosthenes for speed instead of testing each number individually. If the UI becomes slow when adding thousands of items, consider adding results to a TStringList first or paging the output.

Why do you test the number to see if it is odd and then test to see if it is even? If it isn't odd it must be even, no?

For a simple test for whether a number is prime, see:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.