1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

What does this pascal code do?

  1. Dec 15, 2016 #1
    1. The problem statement, all variables and given/known data
    A small list of business firms have their own registar number which is between 10000 and 30000. The first 4 digits represent the firm and the fifth digit is the control digit. The number is valid if the sum of all its digits are divisible by 7. Create a program which checks this.
    Code (Pascal):

    program reg_num(input, output);
    var
      i, num, sum:integer;
    begin
      write(output, 'Enter a number:');
      read(input, num);
      if (num<10000) or (num>30000) then
          writeln(output, 'Not Correct')
      else
        begin
          sum:=0;
          for i:=1 to 5 do
          begin
            sum:=sum + num mod 10;
            num:=num div 10
          end;
        if sum mod 7=0 then
            writeln(output, 'Correct')
        else
            writeln(output, 'Not Correct')
          end;
    end.
     
    2. Relevant equations
    3. The attempt at a solution

    Everything is clear except for theis part:
    Code (Pascal):

    begin
          sum:=0;
          for i:=1 to 5 do
          begin
            sum:=sum + num mod 10;
            num:=num div 10
          end;
     
    How does pascal divide the numbers into digits to add them up? And how to we increment i? In python you have to put i+=1 to increment it but here there is no sich part? How does it keep track?
     
  2. jcsd
  3. Dec 15, 2016 #2

    jedishrfu

    Staff: Mentor

    Google is your friend, you should first look these up before asking.

    The for loop increments by 1 in this example.

    The num div 10 is the key here. It extracts the units digit from the number and the num div 10 strips it so the number
    12345 with num mod 10 you'd get 5 and then the num div 10 returns 1234.
     
  4. Dec 15, 2016 #3
    I believe you understand what the div and mod operations do, so lets take an example say num=12345 (twelve thousand three hundred and forty five).
    sum=sum+12345 mod 10 will give us sum=sum+ 5 which 5 is the last digit, . Then num=12345 div 10 so num=1234. We then go back again to sum=sum+num mod 10, so sum=5+(1234 mod 10)=5+4=9, so we have the sum of the last two digits and so on...

    The increment in i is done automatically by the pascal "everytime it meets the 'end' statement" you don't have to write anything about it. if you want to have an increment other than +1 you have to declare it in the for ... do statement with the step command (if I remember well, its long I programed in pascal))(like for I=1 to 5 step 2 do).
     
  5. Dec 17, 2016 #4

    NascentOxygen

    User Avatar

    Staff: Mentor

    You have a compiler? So why not run this code and see how it works? Insert some write/writeln statements so you can monitor values of variables within the loop and after each statement executes.

    holly-1756.gif
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted