Write a MATLAB function named isDiv11 to receive a number as

Write a MATLAB function named isDiv11 to receive a number as input, and determine if it is divisible by 11. The output argument is a logical—1 (true) if the number is divisible by 11 and 0 (false) if it is not.

Example: >> isDiv11(92)

ans = 0 >>

isDiv11(7480)

ans = 1

Code Requirements: No division is allowed! Instead determine if the number is divisible by 11 using this rule: Take the alternating sum of the digits in the number, reading from left to right. If that is divisible by 11, so is the original number. So, for instance, 2728 has alternating sum of digits 2-7+2-8 = –11. The number 91818181818, which has an alternating sum of 44, is also divisible by 11. Additionally, if the alternating sum is zero, it is divisible by 11. No MATLAB functions that can be used to determine divisibility (such as rem) are permitted

No vectorization is allowed! Do not use any functions, such as sum, any, or find, that work on an entire array. This includes use of the colon operator (except in a for loop) *****Violation of these requirements will result in a grade of zero for this part of the assignment.

******* Input Restrictions & special cases: The function should halt and return a red error message for the following conditions.

• Any input is nonscalar

• Any input is not a positive integer

• Any input is greater than or equal to 1x1015

Hint Built-in functions num2str and str2num will be useful to get to the individual digits of the input number. They are discussed in Chapter 7 of your book, and in MATLAB help. Publish the first sample cases as pdf. Upload files isDiv11.m & isDiv11.pdf

Solution

Matlab Code

function [out] = isDiv11(x)
A = num2str(x);
sum =0;
for i=1:length(A)
    sum = sum + (-1)^(i+1)*str2num(A(i));
end
n = sum/11;
if n==fix(n)
    out=1;
else
    out=0;
end
  
end

Write a MATLAB function named isDiv11 to receive a number as input, and determine if it is divisible by 11. The output argument is a logical—1 (true) if the num

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site