Skip to main content

Program in matlab to check if the given number is even or odd

Program

Write a program in matlab to check whether the given number is even or odd. Use the function name isevenorodd.

Solution

Solution to this example is very simple. Here we will just use very basic concepts to solve this problem. Have a look at the program and if you don't understand any then go to explanation section and if you still don't understand then go to the below links to check the concept( or basics) behind this program. The concepts are as follows:
1) Matlab functions
2) if else in matlab
3) rem function in matlab (remainder after division)
4) fprintf in matlab

Program


function isevenorodd(n)
%This function will check whether the given number is even or odd and
%will print even if the number is even and odd if the number is odd.

%Here we are using the remainder function with if else condition statement

if rem(n,2) == 0
    fprintf('The number is even\n');
else
    fprintf('The number is odd\n');
end

To download this program in matlab file format then click here: isevenorodd.m(350 Bytes)

Explanation

We will understand this program with an example so that everyone will get it easily. Here I am considering the number 5.
First in the function you will give the input 5 as n.
Now digging deep, the if else statement is started and the condition here to satisfy the if statement is, if the remainder after division with the number 2 is 0 then you can go inside the if statement, otherwise you are not allowed to access the statement inside the if statement, and you will have to check the next condition if there or else go to else. The condition is not satisfied. Now here there is no else if statement and the program will directly reach the else statement. In the else statement there is a fprintf statement to print the output as the number is odd. So finally the output is as follows:
>> The number is odd
>>

I have tried to explain the program in such a way that it will be easy for everyone to understand, if you don't understand anything or if you have any doubt, comment below or you can also contact me here: contact me

The above code was high lighted using hilite.me
Understood the code?? Then run the code on your machine and comment your input and output below in the comments.
Keywords: even, odd, matlab functions, if-else, check

Popular posts from this blog

Project Euler Problem 67 Solution with Python

Maximum path sum II By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.

Problem 60 Project Euler Solution with python

Prime pair sets The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. This problem is j u st a brute force problem. If you have come here because you don't know the limit upto which you will h ave to gener ate the prime numbers t hen go ahe ad and t r y with 10,000 . When I first start ed solving the problem I chose 1 million(beca use most of the problem s on project E uler have this limit ), but it took very long for the computer to fin d the solution. After searching on the internet then I found many people choosing 10, 000 so I have changed my in put f rom 1 million to 10000 and the output was f ast. He...

Project Euler Problem 66 Solution with python

Diophantine equation ¶ Consider quadratic Diophantine equations of the form: $$ x^{2} – Dy^{2} = 1 $$ For example, when $D = 13$, the minimal solution in $ x $ is $ 649^{2} – 13 \times 180^{2} = 1 $ It can be assumed that there are no solutions in positive integers when $ D $ is square. By finding minimal solutions in $ x $ for $ D = {2, 3, 5, 6, 7} $, we obtain the following: $$ 3^{2} – 2×2^{2} = 1 $$ $$ 2^{2} – 3×1^{2} = 1 $$ $$ 9^{2} – 5×4^{2} = 1 $$ $$ 5^{2} – 6×2^{2} = 1 $$ $$ 8^{2} – 7×3^{2} = 1 $$ Hence, by considering minimal solutions in $ x $ for $ D ≤ 7 $, the largest $ x $ is obtained when $ D = 5 $. Find the value of $ D ≤ 1000 $ in minimal solutions of $ x $ for which the largest value of $ x $ is obtained.