How to resolve the algorithm Even or odd step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Even or odd step by step in the BASIC programming language

Table of Contents

Problem Statement

Test whether an integer is even or odd. There is more than one way to solve this task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Even or odd step by step in the BASIC programming language

Source code in the basic programming language

10 INPUT "ENTER A NUMBER: ";N
20 IF N/2 <> INT(N/2) THEN PRINT "THE NUMBER IS ODD":GOTO 40
30 PRINT "THE NUMBER IS EVEN"
40 END


' Even or odd
OPTION MEMTYPE int
SPLIT ARGUMENT$ BY " " TO arg$ SIZE dim
n = IIF$(dim < 2, 0, VAL(arg$[1]))
PRINT n, " is ", IIF$(EVEN(n), "even", "odd")

for i = 1 to 10
  if (i mod 2) then print i;" is odd" else print i;" is even"
next i
end

      IF FNisodd%(14) PRINT "14 is odd" ELSE PRINT "14 is even"
      IF FNisodd%(15) PRINT "15 is odd" ELSE PRINT "15 is even"
      IF FNisodd#(9876543210#) PRINT "9876543210 is odd" ELSE PRINT "9876543210 is even"
      IF FNisodd#(9876543211#) PRINT "9876543211 is odd" ELSE PRINT "9876543211 is even"
      END

      REM Works for -2^31 <= n% < 2^31
      DEF FNisodd%(n%) = (n% AND 1) <> 0

      REM Works for -2^53 <= n# <= 2^53
      DEF FNisodd#(n#) = n# <> 2 * INT(n# / 2)


10 cls
20 for n = 1 to 10
30  print n;
40  if (n and 1) = 1 then print "is odd" else print "is even"
50  next n
60 end


10 rem determine if integer is even or odd
20 print "Enter an integer:";
30 input i%
35 print
40 eo$="even"
50 if (i% and 1)=1 then eo$="odd"
60 print "The number ";i%;"is ";eo$;"."

' FB 1.05.0 Win64

Dim n As Integer

Do
  Print "Enter an integer or 0 to finish : ";
  Input "", n
  If n = 0 Then
    Exit Do
  ElseIf n Mod 2 = 0 Then
    Print "Your number is even"
    Print
  Else
    Print "Your number is odd"
    Print
  End if
Loop

End

Public Sub Form_Open()
Dim sAnswer, sMessage As String

sAnswer = InputBox("Input an integer", "Odd or even")

If IsInteger(sAnswer) Then
  If Odd(Val(sAnswer)) Then sMessage = "' is an odd number"
  If Even(Val(sAnswer)) Then sMessage = "' is an even number"
Else
  sMessage = "' does not compute!!"
Endif

Print "'" & sAnswer & sMessage

End

10 INPUT "Enter a number: ", N
20 IF N MOD 2 = 1 THEN PRINT "It is odd." ELSE PRINT "It is even."

100 DEF ODD(X)=MOD(X,2)
110 INPUT PROMPT "Enter a number: ":X
120 IF ODD(X) THEN
130   PRINT X;"is odd."
140 ELSE
150   PRINT X;"is even."
160 END IF

n=12

if n mod 2 = 0 then print "even" else print "odd"

10 REM Even or odd
20 PRINT "Enter an integer number";
30 INPUT N
40 IF N/2 <> INT(N/2) THEN 70
50 PRINT "The number is even."
60 GOTO 80
70 PRINT "The number is odd."
80 END

10 CLS
20 FOR N = -5 TO 5
30 PRINT N;
40 IF (N AND 1) = 1 THEN PRINT "is odd" ELSE PRINT "is even"
50 NEXT N
60 END


;use last bit method
isOdd = i & 1         ;isOdd is non-zero if i is odd
isEven = i & 1 ! 1    ;isEven is non-zero if i is even

;use modular method
isOdd = i % 2         ;isOdd is non-zero if i is odd
isEven = i % 2 ! 1    ;isEven is non-zero if i is even

'This is a comment line. It also could have been preceded with "Rem"

Dim i%          'This line is not necessary, but % strict casts
                'as an Int (2 bytes). "As Int" could have been used instead.
Input "#? ", i% 'Prints "#? " as a prompt and waits
                'for user input terminated by pressing [ENTER].

'Binary integers example
If i% And 1 Then 'Test whether the input value AND 1 is 0 (false) or 1 (true).
                 'There is no global or constant "True" or "False".
    Print "Odd"  'Prints "Odd" if the above tested "true".
Else             'This could have been also been "ElseIf Not (i% And 1)"
    Print "Even" 'Prints "Even in all other cases (Else)
                 'or if the logical inverse of the input value AND 1 tested
                 '"true" (ElseIf).
End If

'Modular congruence example
If i% Mod 2 Then
    Print "Still Odd"
Else
    Print "Still Even"
End If

FOR i = 1 TO 10
  IF i AND 1 THEN PRINT i; " is odd" ELSE PRINT i; " is even"
NEXT i


10 CLS
20 FOR n = -5 TO 5
30  PRINT n;
40  IF n % 2 <> 0 THEN PRINT " is odd" ELSE PRINT " is even"
50  NEXT n
60 END


for i = 1 to 10
  if i and 1 then print i;" is odd" else print i;" is even"
next i

rem - return true (-1) if even, otherwise false (0)
function even(i = integer) = integer
var one = integer    rem - both operands must be variables
one = 1
end = ((i and one) = 0)

rem - exercise the function
var i = integer
for i = 1 to 10 step 3
   print i; " is ";
   if even(i) then
     print "even"
   else
     print "odd"
next

end


If fPart(.5Ans
Then
Disp "ODD
Else
Disp "EVEN
End

10 PRINT "Enter a number:"
20 INPUT N
30 IF 2*(N/2) = N THEN GOTO 60
40 PRINT "It's odd."
50 END
60 PRINT "It's even."
70 END


FOR i = 1 to 10
    IF MOD(i, 2) = 0 THEN PRINT i; " is odd" ELSE PRINT i; " is even"
NEXT i
END


Option Explicit

Sub Main_Even_Odd()
Dim i As Long

    For i = -50 To 48 Step 7
        Debug.Print i & " : IsEven ==> " & IIf(IsEven(i), "is even", "is odd") _
         & " " & Chr(124) & " IsEven2 ==> " & IIf(IsEven2(i), "is even", "is odd") _
         & " " & Chr(124) & " IsEven3 ==> " & IIf(IsEven3(i), "is even", "is odd") _
         & " " & Chr(124) & " IsEven4 ==> " & IIf(IsEven4(i), "is even", "is odd")
    Next
End Sub

Function IsEven(Number As Long) As Boolean
'Use the even and odd predicates
    IsEven = (WorksheetFunction.Even(Number) = Number)
End Function

Function IsEven2(Number As Long) As Boolean
'Check the least significant digit.
'With binary integers, i bitwise-and 1 equals 0 iff i is even, or equals 1 iff i is odd.
Dim lngTemp As Long
    lngTemp = CLng(Right(CStr(Number), 1))
    If (lngTemp And 1) = 0 Then IsEven2 = True
End Function

Function IsEven3(Number As Long) As Boolean
'Divide i by 2.
'The remainder equals 0 if i is even.
Dim sngTemp As Single
    sngTemp = Number / 2
    IsEven3 = ((Int(sngTemp) - sngTemp) = 0)
End Function

Function IsEven4(Number As Long) As Boolean
'Use modular congruences
    IsEven4 = (Number Mod 2 = 0)
End Function

Function odd_or_even(n)
	If n Mod 2 = 0 Then
		odd_or_even = "Even"
	Else
		odd_or_even = "Odd"
	End If
End Function

WScript.StdOut.Write "Please enter a number: "
n = WScript.StdIn.ReadLine
WScript.StdOut.Write n & " is " & odd_or_even(CInt(n))
WScript.StdOut.WriteLine

Module Module1

    Sub Main()
        Dim str As String
        Dim num As Integer
        While True
            Console.Write("Enter an integer or 0 to finish: ")
            str = Console.ReadLine()
            If Integer.TryParse(str, num) Then
                If num = 0 Then
                    Exit While
                End If
                If num Mod 2 = 0 Then
                    Console.WriteLine("Even")
                Else
                    Console.WriteLine("Odd")
                End If
            Else
                Console.WriteLine("Bad input.")
            End If
        End While
    End Sub

End Module


Imports System.Numerics

Module Module1
  Function IsOdd(bi As BigInteger) As Boolean
    Return Not bi.IsEven
  End Function

  Function IsEven(bi As BigInteger) As Boolean
    Return bi.IsEven
  End Function

  Sub Main()
    ' uncomment one of the following Dim statements
    ' Dim x As Byte = 3
    ' Dim x As Short = 3
    ' Dim x As Integer = 3
    ' Dim x As Long = 3
    ' Dim x As SByte = 3
    ' Dim x As UShort = 3
    ' Dim x As UInteger = 3
    ' Dim x As ULong = 3
    ' Dim x as BigInteger = 3
    ' the following three types give a warning, but will work
    ' Dim x As Single = 3
    ' Dim x As Double = 3
    ' Dim x As Decimal = 3

    Console.WriteLine("{0} {1}", IsOdd(x), IsEven(x))
  End Sub
End Module


PROGRAM	"Even/Odd"

DECLARE FUNCTION  Entry ()

FUNCTION  Entry ()
  FOR i = 1 TO 10
    IF (i MOD 2) THEN PRINT i;" is odd" ELSE PRINT i;" is even"
  NEXT i
END FUNCTION

END PROGRAM

for i = -5 to 5
    print i, and(i,1), mod(i,2)
next

10 FOR n=-3 TO 4: GO SUB 30: NEXT n
20 STOP
30 LET odd=FN m(n,2)
40 PRINT n;" is ";("Even" AND odd=0)+("Odd" AND odd=1)
50 RETURN
60 DEF FN m(a,b)=a-INT (a/b)*b

  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Terminal control/Inverse video step by step in the Perl programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the Ruby programming language
You may also check:How to resolve the algorithm N-queens problem step by step in the C programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the Objeck programming language