13.07.2015 Views

A practical introduction to Pascal programming language - GIARA

A practical introduction to Pascal programming language - GIARA

A practical introduction to Pascal programming language - GIARA

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A Practical Introduction <strong>to</strong> <strong>Pascal</strong> Programming Languagewrite(a,’, ’);b:=1;write(b,’, ’);(* Now comes the sequence from second position on *)for i:=3 <strong>to</strong> num dobeginaux:=a+b;a:=b;b:=aux;write(b,’, ’)endend.⊡Exercise 3.7. Write a program that asks the user for 2 integer numbers, the second being greaterthan the first one. Then, displays all the numbers existing between the first and the second one(both included), in decreasing order.□In this exercise we have <strong>to</strong> use a for statement <strong>to</strong> go though the numbers. The clause used<strong>to</strong> modify the counting variable is down<strong>to</strong>, since the sequence has <strong>to</strong> be decreasing.Program 45: Exercise 3.7program c03e07;vara,b,i:integer;beginwrite(’Please input the first integer: ’);readln(a);write(’Please input the second integer: ’);readln(b);for i:=b down<strong>to</strong> a dowrite(i,’ ’);(* The space separates the numbers *)writeln(* Jumping the line *)end.⊡Exercise 3.8. Write a program that asks the user for 2 integer numbers, the second being greaterthan the first one. Then, displays all the numbers existing between the first and the second one(both included). First, all the odd numbers (including the numbers input by the user) mustappear in decreasing order in the same line. Then, all the even numbers (including the numbersinput by the user) must appear in another line in increasing order.□In this exercise we have <strong>to</strong> go twice through the numbers in the sequence of numberscontained between the values input by the user. The first time, we must traverse the sequence indecreasing order, displaying all the odd numbers. Then, we make the way in increasing order,displaying the even ones. Both of the tasks fits a for statement, the difference being the fact thatthe first one uses a down<strong>to</strong> clause, while the second uses a <strong>to</strong>. To complete the exercise we need<strong>to</strong> consider the use of mod 2 for discriminating even and odd numbers.44

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!