locked
For...EndFor RRS feed

  • Question

  • Steps involved in finding the square of even numbers from 15 to 30.
    Sunday, August 21, 2016 2:58 PM

Answers

  • ... in SmallBasic:

    For n = 15 To 30
      If Math.Remainder(n,2) = 0 Then
        TextWindow.WriteLine(n +"² = "+ n * n)
      EndIf
    EndFor
    TextWindow.WriteLine("")

    • Marked as answer by litdev Sunday, August 28, 2016 9:19 AM
    Sunday, August 21, 2016 3:15 PM
    Answerer
  • Yes, see my answer.

    For n = 16 to 30 step 2
        TextWindow.WriteLine(n +"² = "+ n * n)
    EndFor


    Jan [ WhTurner ] The Netherlands


    • Edited by WhTurner33Editor Sunday, August 21, 2016 4:03 PM
    • Proposed as answer by Nonki Takahashi Monday, August 22, 2016 2:30 AM
    • Marked as answer by litdev Sunday, August 28, 2016 9:18 AM
    Sunday, August 21, 2016 4:02 PM
    Answerer
  • PS:

    If you want to use 'Step 2' in the For-To loop and want to be on the save side, weather to start from 15 or 16, you should use a small modificaion

    n1 = 15 ' 16
    n2 = 30
    
    n1 = 2 * Math.Round((n1+1) / 2)  '' -> n1 if even OR next even number
    For n = n1 To n2 Step 2
      TextWindow.WriteLine(n +"² = "+ n * n)
    EndFor
    TextWindow.WriteLine("")
    where it does'nt matter if you start from 15 or 16, and covers oth cases.

    • Marked as answer by litdev Sunday, August 28, 2016 9:18 AM
    Monday, August 22, 2016 4:42 PM
    Answerer

All replies

  • ... in SmallBasic:

    For n = 15 To 30
      If Math.Remainder(n,2) = 0 Then
        TextWindow.WriteLine(n +"² = "+ n * n)
      EndIf
    EndFor
    TextWindow.WriteLine("")

    • Marked as answer by litdev Sunday, August 28, 2016 9:19 AM
    Sunday, August 21, 2016 3:15 PM
    Answerer
  • Better:

    For n = 16 to 30 step 2

    The If ... Endif  can then be left out.


    Jan [ WhTurner ] The Netherlands

    Sunday, August 21, 2016 3:26 PM
    Answerer
  • Thanks. But I have not learnt Math.Remainder function. Can it be solved without using this?

    Regards

    Sunday, August 21, 2016 3:37 PM
  • Yes, see my answer.

    For n = 16 to 30 step 2
        TextWindow.WriteLine(n +"² = "+ n * n)
    EndFor


    Jan [ WhTurner ] The Netherlands


    • Edited by WhTurner33Editor Sunday, August 21, 2016 4:03 PM
    • Proposed as answer by Nonki Takahashi Monday, August 22, 2016 2:30 AM
    • Marked as answer by litdev Sunday, August 28, 2016 9:18 AM
    Sunday, August 21, 2016 4:02 PM
    Answerer
  • If Math.Remainder(n,2) = 0 Then

     ... is the same as ...

    If n/2 = Math.Round(n/2) Then    ' or

    If n/2 = Math.Floor(n/2) Then

    and means that 'If the current number is divisible by 2 without rest'  ... so is a even number.

    So, if you have to begin with 15, you can also use

    For n = 15 To 30
      If n/2 = Math.Round(n/2) Then ' If Math.Remainder(n,2) = 0 Then
        TextWindow.WriteLine(n +"² = "+ n * n)
      EndIf
    EndFor




    Sunday, August 21, 2016 4:25 PM
    Answerer
  • my variation:

    CUBE of  numbers 1 to 100

    For n = 1 To 100 Step 3
      TextWindow.Write (" cube of " + n + " = " + n*n*n)
      TextWindow.WriteLine ("") ' linefeed
    EndFor

    Sunday, August 21, 2016 6:53 PM
    Answerer
  • PS:

    If you want to use 'Step 2' in the For-To loop and want to be on the save side, weather to start from 15 or 16, you should use a small modificaion

    n1 = 15 ' 16
    n2 = 30
    
    n1 = 2 * Math.Round((n1+1) / 2)  '' -> n1 if even OR next even number
    For n = n1 To n2 Step 2
      TextWindow.WriteLine(n +"² = "+ n * n)
    EndFor
    TextWindow.WriteLine("")
    where it does'nt matter if you start from 15 or 16, and covers oth cases.

    • Marked as answer by litdev Sunday, August 28, 2016 9:18 AM
    Monday, August 22, 2016 4:42 PM
    Answerer