Sunday, October 14, 2018

Pascal program to print prime numbers

Prime numbers

The number that is divisible only by itself and 1.
Pascal program to print prime numbers between 1 and hundred.

 
{Program to print prime numbers between 1 and 100}
program PrimeNumber;
const
  Num=100;
var
  i,j:integer;
  prime:boolean;

begin
  i:=2;
  while i<=Num do
  begin
     j:=2;
     prime:=True;
     while j<i do {to make it efficient here can use j<=i/2 :Reason: last factor of any number is number itself one before last number is always less than half of the value  }
     begin
        if i mod j= 0 then
        begin
          prime:=False;
        end;
        j:=j+1;
     end;
     if prime=True then
     begin
       writeln(i);
     end;
     i:=i+1;
  end;
  writeln('Press Enter to Exit....');
  readln;
end.

Output of the above Pascal Program


PascalProgramOutputofPrimeNumbers



2 comments: