473,473 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB.NET > For ... Next Repetition Statement

1 New Member
Write a program that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single carriage return. Maximize your use of repetition (with nested For … Next statements) and minimize the number of output statements.

Expand|Select|Wrap|Line Numbers
  1.     *
  2.    ***
  3.   *****
  4.  *******
  5. *********
  6.  *******
  7.   *****
  8.    ***
  9.     *
__________________________________________________ _______________

I am having trouble doing this problem using "For ... Next" Repetition Statement.

I can use Console.WriteLine("*****") but I need to use repetition statements inorder to minimize the number of output statements.

My updated code is this:
Expand|Select|Wrap|Line Numbers
  1. Module ForCounter
  2. Sub Main()
  3. Dim i As Integer
  4. Dim j As Integer
  5.  
  6. For i = 0 To 3
  7. Console.Write("*")
  8. Next
  9. For j = 0 To 5
  10. Console.WriteLine("*")
  11. Next
  12.  
  13. End Sub ' Main
  14. End Module
Which results in this:

*****
*
*
*
*
*

Clearly not a diamond

Please HELP!!!
Jun 11 '06 #1
6 34214
Banfa
9,065 Recognized Expert Moderator Expert
3 nested for loops should do it

Expand|Select|Wrap|Line Numbers
  1. For each line
  2.     For number of spaces required at beginning of line
  3.         write space
  4.     Next
  5.     For number of stars on line
  6.         write star
  7.     Next
  8.     write new line
  9. Next
  10.  
Jun 11 '06 #2
vavjeeva
1 New Member
try this

Expand|Select|Wrap|Line Numbers
  1. int iCurrIndex = -1;
  2.             decimal iMax = 9;
  3.             string strControl = "ADD";
  4.             for(int i=1;i<10;i++)
  5.             {
  6.                 if(iCurrIndex == iMax)
  7.                     strControl = "SUB";                                    
  8.  
  9.                 if (strControl == "ADD")
  10.                     iCurrIndex+=2;
  11.                 else
  12.                     iCurrIndex-=2;
  13.  
  14.                 Console.WriteLine(String.Empty.PadLeft(Convert.ToInt32((iMax - iCurrIndex) / 2), ' ') 
  15.                     + String.Empty.PadLeft(iCurrIndex, '*'));
  16.             }
  17.  
Jul 18 '07 #3
Killer42
8,435 Recognized Expert Expert
How about something a bit simpler...

Expand|Select|Wrap|Line Numbers
  1. Dim S As Integer
  2. Dim A As Integer
  3. S = 5
  4. For I = 1 to 5
  5.   A = A + 1
  6.   S = S - 1
  7.   For J = 1 To S ' Spaces
  8.     Console.Write(" ")
  9.   Next
  10.   For J = 1 To A ' Asterisks
  11.     Console.Write("*")
  12.   Next
  13.   Console.WriteLine
  14. Next
  15. ' Then do the same in reverse...
  16. For I = 1 to 4
  17.   A = A - 1
  18.   S = S + 1
  19.   For J = 1 To S ' Spaces
  20.     Console.Write(" ")
  21.   Next
  22.   For J = 1 To A ' Asterisks
  23.     Console.Write("*")
  24.   Next
  25.   Console.WriteLine
  26. Next
  27.  
I use VB6, so my Write/WriteLine syntax may be off.
Jul 19 '07 #4
Killer42
8,435 Recognized Expert Expert
...for(int i=1;i<10;i++)
Wow! I knew MS had worked hard to try and turn VB.Net into Java, but didn't realise they had gone that far.
Jul 19 '07 #5
cyberdaemon
38 New Member
Wow! I knew MS had worked hard to try and turn VB.Net into Java, but didn't realise they had gone that far.
Could not help myself...ROTFLOL!!!

I apologize if I offend anyone, but that is just funny!!!

Cyberdaemon
Jul 19 '07 #6
goldi
1 New Member
Write a program that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single carriage return. Maximize your use of repetition (with nested For … Next statements) and minimize the number of output statements.

Expand|Select|Wrap|Line Numbers
  1.     *
  2.    ***
  3.   *****
  4.  *******
  5. *********
  6.  *******
  7.   *****
  8.    ***
  9.     *
__________________________________________________ _______________

I am having trouble doing this problem using "For ... Next" Repetition Statement.

I can use Console.WriteLine("*****") but I need to use repetition statements inorder to minimize the number of output statements.

My updated code is this:
Expand|Select|Wrap|Line Numbers
  1. Module ForCounter
  2. Sub Main()
  3. Dim i As Integer
  4. Dim j As Integer
  5.  
  6. For i = 0 To 3
  7. Console.Write("*")
  8. Next
  9. For j = 0 To 5
  10. Console.WriteLine("*")
  11. Next
  12.  
  13. End Sub ' Main
  14. End Module
Which results in this:

*****
*
*
*
*
*

Clearly not a diamond

Please HELP!!!
>>>>>>>>ammm, are you done doing this program..?????
>>>post your correct code if you've done doing it
>>>>>>i just want to see it
>>>>thanks
Feb 23 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element...
61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
3
by: KemperR | last post by:
Hello Experts outhere, may be someone can tell me whats going wrong with my ADOX trial. I have an Access 2002 database with some tables and queries (views) The code listed below works well up...
2
by: Deniz Bahar | last post by:
Hi, I'm working with a single linked list and want to delete elements by searching through the list (starting form the HEAD) then finding the element, then doing the following: NewElement =...
8
by: vijay | last post by:
Hello, As the subject suggests, I need to print the string in the reverse order. I made the following program: # include<stdio.h> struct llnode { char *info;
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
11
by: Holger | last post by:
Hi I have not been able to figure out how to do compound statement from C - "<test>?<true-val>:<false-val>" But something similar must exist...?! I would like to do the equivalent if python...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
6
by: Kbalz | last post by:
Trying to run a simple query on my dataset the SQL statement looks like this: select * from person where id in (@p) When using more than one id in the "in statement", I get...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.