0% found this document useful (0 votes)
3 views4 pages

Gr11 For Loop Factors Etc

Uploaded by

shivayramdhin1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Gr11 For Loop Factors Etc

Uploaded by

shivayramdhin1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

KHARWASTAN SECONDARY

INFORMATION TECHNOLOGY
GRADE 10/11

TOPIC: for loop continued…

Questions: N.B. design a project with one button and one richedit for each of
the following.

1. Input an integer and list all factors of the integer.

procedure TForm1.btnFactorsClick(Sender: TObject);


var
inum, i: integer;
begin
//INPUT
inum:= strtoint(inputbox('Number','Enter a number','0'));
//DISPLAY THE NUMBER
redout.Lines.Add('Number: '+inttostr(inum));
redout.Lines.Add('Factors:');

for i := 1 to inum do
begin
if inum mod i = 0 then
begin
redout.Lines.Add(inttostr(i));
end; //if
end; //loop
end;
2. Input an integer and determine if it is a prime number or not.

procedure TForm1.btnPrimeClick(Sender: TObject);


var
inum, i ,ifactors: integer;

begin
//INPUT
inum:= strtoint(inputbox('Number','Enter a number','0'));
//DISPLAY THE NUMBER
redout.Lines.Add('Number: '+inttostr(inum));
redout.Lines.Add('Factors:');

//intialise a counter to count the number of factors


ifactors:= 0;

for i := 1 to inum do
begin
if inum mod i = 0 then
begin
inc(ifactors); //count factors
redout.Lines.Add(inttostr(i)); //output factors
end; //if
end; //loop

//test for prime


if ifactors = 2 then
begin
redout.Lines.Add(inttostr(inum)+' is a prime number');
end //if
else
begin
redout.Lines.Add(inttostr(inum)+' is a composite number');
end; //else

end;
3. Input an integer and determine if the integer is a special number. A special number is
a number where the sum of its factors excluding itself is equal to itself.
e.g.s of special numbers include 6 and 28.

Integer: 6
1+2+3 = 6

procedure TForm1.btnPerfectNumberClick(Sender: TObject);


var
inum, i, isum: integer;
begin
//INPUT
inum:= strtoint(inputbox('Number','Enter a number','0'));
//DISPLAY THE NUMBER
redout.Lines.Add('Number: '+inttostr(inum));
redout.Lines.Add('Factors:');

// INITIALISE ISUM
isum:= 0;

for i := 1 to inum -1 do //dont include the number itself


begin
if inum mod i = 0 then
begin
isum := isum + i;
end; //if
end; //loop

//check if perfect number


if isum = inum then //check if the sum of the factors excluding itself is
equal to itself
begin
redout.Lines.Add(inttostr(inum)+' is a perfect number');
end //if
else
begin
redout.Lines.Add(inttostr(inum)+' is NOT a perfect number');
end; //else

end;
4. Allow the user to enter 2 integers for a range of numbers. Determine and list all
prime numbers that lie inbetween this range. Make sure you enter the smaller
number first and then the large.

procedure TForm1.btnPrimeBetweenNumbersClick(Sender: TObject);


var
inum1,inum2, i, j, ifactors:integer;
begin
inum1:= strtoint(inputbox('Number','Enter start of range','0')); //must be smaller
number
inum2:= strtoint(inputbox('Number','Enter end of range','0')); //must be larger number

reddisplay.Lines.Add('Prime numbers between '+inttostr(inum1)+' and '+inttostr(inum2));

for i := inum1+1 to inum2 -1 do //outer loop runs between the 2 numbers


begin
//initialise counter to count factors
ifactors := 0;

for j := 1 to i do //inner loop - checking each number between range for factors
begin
if i mod j = 0 then
begin
inc(ifactors);
end; //if
end; //j loop

//checking if prime
if ifactors = 2 then
begin
reddisplay.Lines.Add(inttostr(i));
end; //if
end; //i loop

end;

“AIM FOR EXCELLENCE”…ECM

You might also like