English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

hi! can you help me with this one? By using the nested for loop, make a triangle asterisks like this:
if you input 6, the output will be:
**************************************** 13
*************************** 11
**************** 7
********* 5
**** 3
* 1

2007-09-16 02:37:59 · 3 answers · asked by RAJ 1 in Computers & Internet Programming & Design

the ouput will appear n the listbox

2007-09-16 02:38:59 · update #1

3 answers

I suspect that there is a slight error in the question (unless this is supposed to be a hard problem )

The trick is to create a formula which for a given line number which will provide the number of asterisks to print on a line.

If you look at the numbers 1,3,5,& 7 these are created by
((N-1)*2) + 1 where N is a line number starting at 1
Or
((N)*2)+1 for lines starting at zero.

This progression works until you get to the fifith line where it skips. You can either write a more complicated formula incorporating MODULO and INTEGER division OR use IF statments and a counter to keep track of when to insert a skip in the sequence.

So when counting lines starting at zero you get a progression of:
Line = Result of (N*2)+1
0 = 1
1 = 3
2 = 5
3 = 7
4 = 9
5 = 11
6 = 13

It appears that either the question you have is miscopied OR that youu have to skip every 5th line in the series (this is the harder part of the program) Look at integer division which uses the \ ( the / is regular division) and MODULO (MOD)
MOD returns the remainder
SO 7 MOD 5 = 2 (the remainder) and 5 MOD 5 = 0
You can use MOD and an if statment to skip every fifth line when its result is zero
OR you can use the integer division result of n\5 and add it to the line numer then crank the result through your formula

Some integer division results
6\5 = 1
5\5 = 1
4\5 = 0
3\5 = 0


Printing a series of asterisks is done by string concatenation

Dim str as string
dim n as integer
n = 6 ' this is the number of * to print
str = "" ' clear string
for x = 1 to n
str = str & "*"
Next

this loop just sticks a series of * together


Take the above loop an nest it inside a For next loop which generates line numbers from high to low

For LineNum = 6 to 1 step -1
'Place code here to compute the number of asterisks

Place code here (see above) to print asterisks on a line

Next

2007-09-16 03:31:28 · answer #1 · answered by MarkG 7 · 1 0

hi, sorry i dont know vb .net but i suppose the pseudo code for that would be like

read arg (i.e. 6)

for(int i = 0; i < arg; i++)
{
for(int j = ((arg - i) * 2)+1; j > 0; j--)
{
print an asterisk
}
print a newline character
}

2007-09-16 02:50:32 · answer #2 · answered by gh0stb0y013 2 · 0 0

often for this exercising loops are allowed for the printing of the easily line, as follows: void trend(int n) { if (n > 0) { for (int i = a million; i <= n; i++) { printf("*"); } printf("n"); trend(n - a million); for (int i = a million; i <= n; i++) { printf("*"); } printf("n"); } }

2017-01-02 06:39:11 · answer #3 · answered by lindeen 3 · 0 0

fedest.com, questions and answers