What does this pascal code do?

In summary, the purpose of this pascal code is to execute a specific set of instructions in order to solve a problem or perform a task. It works by following a sequence of commands and logical operations written in the Pascal programming language. The syntax refers to the specific rules and structure that must be followed for the code to be properly written and executed. Each line of code is a specific instruction or command that tells the program what to do, including assigning values to variables, performing calculations, or making decisions. The output of the code is the result of its execution, which may be a specific value or data, or the completion of a task or problem-solving.
  • #1
doktorwho
181
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
  • #2
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
  • #3
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
  • #4
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

Related to What does this pascal code do?

1. What is the purpose of this pascal code?

The purpose of this pascal code is to execute a specific set of instructions in order to solve a problem or perform a task.

2. How does this pascal code work?

This pascal code works by following a sequence of commands and logical operations that are written in the Pascal programming language. These commands and operations are designed to manipulate data and perform calculations.

3. What is the syntax of this pascal code?

The syntax of this pascal code refers to the specific rules and structure that must be followed in order for the code to be properly written and executed. This includes correct usage of keywords, punctuation, and data types.

4. Can you explain what each line of this pascal code means?

Yes, each line of pascal code is a specific instruction or command that tells the program what to do. It may involve assigning values to variables, performing calculations, or making decisions based on certain conditions.

5. What is the output of this pascal code?

The output of this pascal code is the result of the program's execution. This may be a specific value or data, or it could be the completion of a task or solving of a problem.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top