## Printing patterns in Freebasic

New to FreeBASIC? Post your questions here.
ganache
Posts: 31
Joined: Aug 04, 2016 9:25

### Printing patterns in Freebasic

I am writing programs for printing patterns,just for fun.
I want to write a program to output an equilateral triangle(all sides are equal).
Below is the C code. How do I translate the for loops in Freebasic.

#include <stdio.h>
int main()
{
int i, j, n;
//Reads number of rows to be printed
printf("Enter value of n : ");
scanf("%d", &n);

for(i=1; i<=n; i++)
{
//Prints trailing spaces
for(j=i; j<n; j++)
{
printf(" ");
}

//Prints the pyramid pattern
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
BasicCoder2
Posts: 2807
Joined: Jan 01, 2009 7:03

### Re: Printing patterns in Freebasic

Code: Select all

`dim as integer n'Reads number of rows to be printedinput "Enter value of n : ";nfor i as integer = 1 to n    'Prints trailing spaces    for j as integer = i to n-1        print " ";    'semi colon prevents crlf    next j    'Prints the pyramid pattern    for j as integer = 1 to 2*i-1        print "*";    next j        print         'line feed    next iprintprint "Tap key to end"sleep   'wait for key event`
ganache
Posts: 31
Joined: Aug 04, 2016 9:25

### Re: Printing patterns in Freebasic

Thanks BasicCoder2!
Still coming to grips with converting c code to FB code.
ganache
Posts: 31
Joined: Aug 04, 2016 9:25

### Re: Printing patterns in Freebasic

Hi, me again.
I am trying to print different star patterns. BasicCoder2 had helped me with the equilateral triangle.
Here is the c code for printing a hollow square shape and below is my attempt.

' c code
#include<stdio.h>
int main(){
int side, i, j;

printf("Enter side of square\n");
scanf("%d", &side);

/* Row iterator for loop */
for(i = 0; i < side; i++){
/* Column iterator for loop */
for(j = 0; j < side; j++){
/* Check if currely position is a boundary position */
if(i==0 || i==side-1 || j==0 || j==side-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}

' FB code for hollow square
Dim As Integer n, i, j
input "Enter side of square:";n
' Row iterator for loop
for i = 0 to n
'Column iterator for loop
for j = 0 to n-1
' Check if currently position is a boundary position
if (i=0 or i=n-1 or j=0 or j=n-1) then print "*";
next j
print " ";
next i
Sleep
End
BasicCoder2
Posts: 2807
Joined: Jan 01, 2009 7:03

### Re: Printing patterns in Freebasic

Code: Select all

`dim as integer sideinput "Enter side of square";sideprintfor i as integer = 0 to side-1    for j as integer =  0 to side-1        if i=0 or i = side-1 or j=0 or j=side-1 then            print "*";        else            print " ";        end if        next j    printnext isleep`
ganache
Posts: 31
Joined: Aug 04, 2016 9:25

### Re: Printing patterns in Freebasic

Thanks once again BasicCoder2!
MrSwiss
Posts: 1921
Joined: Jun 02, 2013 9:27
Location: Switzerland

### Re: Printing patterns in Freebasic

A speed improved version, which also corrects *out of range* user input.

Code: Select all

`Dim As UInteger side, jInput "Enter side of square: ", side    ' get user inputPrint   ' CrLf = 'empty' line' set min. size, with at least one 'empty' fieldIf side < 3 Then side = 3' set max. size (22 lines, we've used 2 up to now)If side > 22 Then side = 22' pre-calculate variables used by programs 'for-loop'Var smax  = side - 1    ' = integer (by assignment)Var empty = side - 2    ' as aboveFor j = 0 to smax   ' print it: a line at the time (speed)    If j = 0 OrElse j = smax Then        Print String(side, "*") ' top and bottom lines only    Else        Print "*" + String(empty, " ") + "*" ' inbetween lines    End IfNextSleep`
@ganache,

plse. put posted code in future, inside code-tags. Use the code-button above 'text-entry-box'.

### Who is online

Users browsing this forum: No registered users and 1 guest