none
Coding for displaying odd and even numbers

    Question

  • Please help to solve the following. Write a program in Visual Basic 2008 that displays the even numbers between 1 to 9 using:-

    (a)  For ... Next

    (b)  Do While ... Loop

    (c)  Do ... Loop While

    The program should be written in 3 subroutines. 

    Thursday, November 21, 2013 8:23 AM

Answers

  • Hello OP:

    I checked my codes and change to this following:

    Imports System.Globalization
    Imports System.IO
     
    Module Module1
     
        Sub Main()
            'Method A
            Console.WriteLine("Method A:")
            For j As Integer = 1 To 9
                If (j Mod 2 = 0Then
                    Console.Write(j & vbTab)
                End If
            Next
     
            'Method B
            Console.WriteLine(vbCrLf & "Method B:")
            Dim i As Integer = 1
            Do While (i <= 9)
                If (i Mod 2 = 0Then
                    Console.Write(i & vbTab)
                End If
                i += 1
            Loop
     
            'Method C
            Console.WriteLine(vbCrLf & "Method C:")
            i = 1
            Do
                If (i Mod 2 = 0Then
                    Console.Write(i & vbTab)
                End If
                i += 1
            Loop While (i <= 9)
        End Sub
    End Module

    ASP.NET Questions
    Other Discussions
    FreeRice Donate
    Issues to report

    Friday, November 22, 2013 2:09 AM

All replies

  • not 

       If (i\2 = 0)

    but

       If (i mod 2 = 0)

    Thursday, November 21, 2013 9:58 AM
  • Luc and ThankfulHeart,

    I know it is always challenging but we help nobody with this if persons simple can find homework assignments on Internet.

    :-)


    Success
    Cor

    • Proposed as answer by Renee Culver Thursday, November 21, 2013 5:18 PM
    Thursday, November 21, 2013 10:23 AM
  • Cor,

    I normally wouldn't have answer this, but I thought that it was worst to give a wrong answer to a student than not answering.

    The OP cannot learn if he/she is shown the wrong way

    Thursday, November 21, 2013 10:57 AM
  • The OP cannot learn if he/she is shown the wrong way

    But this is still wrong:
      Dim i As integer = 1
       Do While(i<=9)
         If(i mod 2 = 0) Then
            Console.WriteLine(i)
         End If
         i+=2
       Loop

    Thursday, November 21, 2013 12:13 PM
  • Coded solutions to homework assignments should NOT be posted AT ALL!  It is up to the student to experiment and discover the solution for him/herself.  You are doing a disservice to the rest of the students in the class by offering an unfair advantage to one student.  All you need to do is offer a hint.  In this case:  How to use the Mod operator.

    Solitaire

    • Proposed as answer by Renee Culver Thursday, November 21, 2013 5:19 PM
    Thursday, November 21, 2013 3:03 PM
  • The OP cannot learn if he/she is shown the wrong way

    But this is still wrong:
      Dim i As integer = 1
       Do While(i<=9)
         If(i mod 2 = 0) Then
            Console.WriteLine(i)
         End If
         i+=2
       Loop


    Oupssss :(
    Thursday, November 21, 2013 3:09 PM
  • Hello,

    When doing an assignment let Google do the work for you along with dissecting each part of code which you found that works. As already mentioned it is common place not to supply solutions as when this is done more times than not the person requesting this information will not  dissect the code and learn from it.

    Example Google search: vb.net odd even. If the right words/tokens are used for simple questions (which this is) then you should get a solution in the first page. Now remember once you have the solution it is your responsibilty to learn the mechanics to what makes the solution work followed by experimenting with a solution. Also when finding solutions that do not work learn why they do not work which is also a good lesson.

    Back to Google we can also do vb.net odd even while.

    So with the above Google links (using my own suggestions) I found a solution for each of your requirements a, b and c so you should too find the proper solution.

    See also, MSDN documentation

    For/Each statement

    Do Loop


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, November 21, 2013 4:26 PM
    Moderator
  • The way to do it is to give NO answer.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Thursday, November 21, 2013 5:29 PM
  • not 

       If (i\2 = 0)

    but

       If (i mod 2 = 0)

    Or If (i And 1) = 0

    But I'm sure the OP can see a pattern in even numbers and write a loop that doesn't even need to check.

    --
    Andrew



    Thursday, November 21, 2013 7:32 PM
  • Hello OP:

    I checked my codes and change to this following:

    Imports System.Globalization
    Imports System.IO
     
    Module Module1
     
        Sub Main()
            'Method A
            Console.WriteLine("Method A:")
            For j As Integer = 1 To 9
                If (j Mod 2 = 0Then
                    Console.Write(j & vbTab)
                End If
            Next
     
            'Method B
            Console.WriteLine(vbCrLf & "Method B:")
            Dim i As Integer = 1
            Do While (i <= 9)
                If (i Mod 2 = 0Then
                    Console.Write(i & vbTab)
                End If
                i += 1
            Loop
     
            'Method C
            Console.WriteLine(vbCrLf & "Method C:")
            i = 1
            Do
                If (i Mod 2 = 0Then
                    Console.Write(i & vbTab)
                End If
                i += 1
            Loop While (i <= 9)
        End Sub
    End Module

    ASP.NET Questions
    Other Discussions
    FreeRice Donate
    Issues to report

    Friday, November 22, 2013 2:09 AM
  • I checked my codes and change to this following:

    If you are answering a homework question then there are still 3 errors.

                    Console.Write(j & vbTab)

                    Console.Write(i & vbTab)

                    Console.Write(i & vbTab)

    Friday, November 22, 2013 3:05 AM
  • Are you pointing at me Acamar?

    If yes, but I tested it ran well on VS2013;)

    Have you tested that yet?

    Thanks again


    ASP.NET Questions
    Other Discussions
    FreeRice Donate
    Issues to report

    Friday, November 22, 2013 3:51 AM
  • If yes, but I tested it ran well on VS2013

    If you are going to write code that you expect a student to hand to their teacher as the answer to an assignment then you have to write it to a much higher standard than 'ran well', or you are only getting the student into more trouble for ignoring the point of the previous day's instruction.  That's why some people won't answer homework questions.

    Friday, November 22, 2013 4:43 AM
  • It's "code" always code, NEVER "codes". 

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Friday, November 22, 2013 5:41 PM
  • And finally the OP has its assignment done

    Good work :)

    Friday, November 22, 2013 6:56 PM