1

Sorry guys I don't know how to copy so i will have to post images. Basically for my assigment i have to use WHILE , i'm making mistake somewhere but i cannot seem to find it. I compiled it it works then i run it , type 5 numbers, (2,14,9,7,1) and it only prints one number divisable by 7. [1]: https://i.stack.imgur.com/ykn2J.png [2]: https://i.stack.imgur.com/G5GAV.png EDIT: i got text so you dont have to check

Program Divisible;
var n,s,i,x:integer;
begin
writeln('How many numbers will you type');
read(n);
begin
s:=0;
I:=1;
end;
while I < n do
begin
writeln('What are those numbers?');
read(x);
If x mod 7 =0 then
begin
s:=x+0;  /help
end;
I:=I+1;
     end;
     writeln('Numbers divisible by 7 are');
writeln(s);
   end.
envison
  • 11
  • 2
  • Please learn to properly indent your code. It makes code easier to read and understand, and will help you immensely in the future when you're attempting to trace the flow of execution to locate a problem. See [this answer](https://stackoverflow.com/a/28221465/62576) for an example of how proper formatting improves readability. – Ken White Oct 14 '20 at 23:37

1 Answers1

1

First of all you need to declare I as 0 because if the amount of numbers is 1 it would exit. Also, you were storing the number directly instead of counting the numbers divisible by seven.

Program Divisible;
var n, s, i, x: integer;
begin
  writeln('How many numbers will you type');
  read(n);
  s:=0;
  I:=0;

  while I < n do
  begin
    writeln('What are those numbers?');
    read(x);
    If x mod 7 = 0 then
    begin
      s := s + 1;
    end;
    I := I + 1;
  end;
  writeln('Numbers divisible by 7 are');
  writeln(s);
end.

If what you need is to show each individual number, you need to declare an array and write the numbers to it, because previously you were overwriting the last number with each loop.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Ale Plo
  • 799
  • 4
  • 11
  • Do you maybe know how to print result of which numbers are divisible by 7 on the end of program. For numbers 2,9,14,7,1. – envison Oct 15 '20 at 14:28
  • This is a little bit trickier because if you use an integer variable it will be overwritten each loop. The best solution would be to declare an array for the integers and if a number is divisible by 7 you add it to this array. – Ale Plo Oct 15 '20 at 17:51