Articles

Solved How to make this triangle in pascal?

November 26, 2012 at 14:52:23
Specs: Windows 7

Since this site helped me so much with my last problem I decided to try again.

*
**
***
****
*****
******

The catch is that you can only use one write and writeln statement in this program. This is my code so far:

 program (input, output);

var
x : integer;

begin 
for x:= 1 to 6 do
  begin
   writeln(' * ');
   write(' *** ');
   end;
end.
    
I know this is wrong, Can anyone give me tips to help solve this problem?

See More: How to make this triangle in pascal?

Report •


#1
November 26, 2012 at 19:20:41

once again, you need nested loops, I believe. Inner loop index from 1 to outer-loop-index "writes", then writeln somewhere between outer-loop and inner-loop (either before inner-loop starts, or after it completes, but inside the outer-loop.) That's the logic.

Report •

#2
November 27, 2012 at 02:22:32
✔ Best Answer

I doubt I am doing the right thing since it seems you did not learn enough from nbrane's hints and your previous homework. Anyway here the solution, but do not rely too much on this site to perform your assignments.

program triangle (input, output);

var
  x, y: integer;

begin 
for x:= 1 to 6 do begin
  for y:= 1 to x do write('*');
   writeln;
end;
end.


Report •

#3
November 28, 2012 at 12:33:51

Thank you for giving me the answer, but can you explain how this loop works?

Report •

Related Solutions

#4
November 29, 2012 at 07:35:16

Pascal was planned to be a highly readable language and indentation is the main tool to achieve the purpose. To indent means to shift toward right a block of statements by two spaces so to highlight the scope of a structure (e.g. a for loop).

In the program there are two nested loops, the outer one driven by x (from 1 to 6) and the inner by y (from 1 to the current value of x). Notice the outer loop embraces two statements, the inner loop (for) and the writeln to issue a line feed/carriage return at the end of each line. The inner loop actually writes the lines so it does not need a begin/end since it is made by just one commad (write('*').

The above logic is the translation in Pascal code of what nbrane suggested in his post #1. The logic of the assignment is exactly the same of the Multiplication Table exercise.


Report •


Ask Question