How Does This Pascal Code Validate a Business Registration Number?

Click For Summary

Discussion Overview

The discussion revolves around understanding a Pascal program that validates a business registration number based on specific criteria. Participants explore how the program processes the number to check its validity, focusing on the use of division and modulus operations to extract digits and calculate their sum.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant seeks clarification on how the Pascal code divides a number into its constituent digits and how the loop increments without explicit increment syntax.
  • Another participant explains that the for loop in Pascal automatically increments the loop variable and describes the role of the div and mod operations in extracting digits from the number.
  • A further explanation is provided using an example to illustrate how the sum is calculated through successive applications of mod and div operations.
  • One participant suggests running the code with additional output statements to observe variable values during execution for better understanding.

Areas of Agreement / Disagreement

Participants generally agree on the functionality of the div and mod operations in Pascal, but there is no consensus on the best approach to learn or verify the program's behavior, as suggestions vary from theoretical explanations to practical experimentation.

Contextual Notes

Some participants assume familiarity with Pascal syntax and programming concepts, which may limit the discussion's accessibility to those less experienced in programming.

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