Today we are going to write a simple program of how to check if a number is even number or odd number . To achieve this we will create a emthod and the method name will be evenNumber and we will pass a argument to check if the number is even or odd . This will be dynamical as we can pass as many arguments we want
Here comes or example .
evenNumber is a method and x is an argument.
In the second line x%2 is this gives us the remainder value , so we can check the value of the remainder when a number is divided by 2. If the remainder is zero then it will be even number
if the remainder is one then it will be even number .
Now after creating the following program, we will call the method and try to execute it by passing a number
Here we are passing evenNumber(2) , this will be called in the method and check in the if condition
2%2 = 0 , we will get the answer as zero , hence it will satisfy the if condition and will print result as even number
Program for your reference
function evenNumber(x,y)
if (x % 2 == 0)
println("x is even ")
else
println("x is odd")
end
end
Program for your reference
function evenNumber(x,y)
if (x % 2 == 0)
println("x is even ")
else
println("x is odd")
end
end
Comments
Post a Comment