How Does This Pascal Code Validate a Business Registration Number?

Click For Summary
The Pascal code validates a business registration number by checking if it falls within the range of 10000 to 30000 and if the sum of its digits is divisible by 7. The program uses a loop to extract each digit of the number using the modulus and division operations. The loop automatically increments the counter variable, i, with each iteration, eliminating the need for explicit increment commands. Users are encouraged to run the code and insert debugging statements to monitor variable values for better understanding. This approach helps clarify how the code processes the number to validate it correctly.
doktorwho
Messages
181
Reaction score
6

Homework Statement


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:
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.

Homework Equations


3. The Attempt at a Solution [/B]
Everything is clear except for theis part:
Code:
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?
 
Physics news on Phys.org
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.
 
  • Like
Likes doktorwho
I believe you understand what the div and mod operations do, so let's 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).
 
  • Like
Likes doktorwho
doktorwho said:
How does pascal divide the numbers into digits to add them up?
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
 
  • Like
Likes doktorwho

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
2
Views
7K