Question

In: Computer Science

Write a function divisibleBy3 with 2 positive integer inputs, a lower bound and an upper bound....

Write a function divisibleBy3 with 2 positive integer inputs, a lower bound and an upper bound. Generate a list of integers from the lower bound to the upper bound and determine how many numbers in the list have a remainder equal to zero when dividing by 3.

Hint: Use a loop and the MATLAB built-in function rem to calculate the remainder after division. The remainder of N divided by P is rem(N,P).

 

Solutions

Expert Solution

In this I have applies a for loop in which the iterator valriable a , starts from lower_bound to upper bound and on each iteration checking whether the number is divisible by 3 or not , by using the rem function and checking if the value returned by rem function is 0 , so then the number will be divisible by 3 , so adding 1 in the result variable.

The code will be :-

lower_bound = 12 % initalising lower bound with random value
upper_bound = 13 % initialising upper bound with random value

function result = divisibleby3(lower_bound,upper_bound) %function calculates the number having remainder 0
result = 0 % initialising result variable with 0
for a = lower_bound:upper_bound % applying a for loop starting from lower_bound till upper_bound
if rem(a,3) == 0 % checking if the remainder is 0 or not
result = result + 1; % if the remainder is 0 adding 1 to result value
end
end
end

result = divisibleby3(12,21) % output will be "result = 4"


Related Solutions

Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
In computer science, when is the right time to find the Upper bound, Lower bound, and...
In computer science, when is the right time to find the Upper bound, Lower bound, and Tight bound? And what does Tight Bound show us?
Given the integral 1/x dx upper bound 2 lower bound 1 (a) use simpson's rule to...
Given the integral 1/x dx upper bound 2 lower bound 1 (a) use simpson's rule to approximate the answer with n=4 Formula:f(x)=1/3[f(x0)+4f(x1)+2f(x2)+...+f(xn)]Δx(keep answer to 6 decimals) b)how large is n in order for the error of Simpsons rule for the given integral is no more than 0.000001 Formula: |Es|=(k)(b-a)^5/(180 n^4), where |f^4(x)≤k| please show all work and steps
Write matlab program to compute ∫f(x)dx lower bound a upper bound b using simpson method and...
Write matlab program to compute ∫f(x)dx lower bound a upper bound b using simpson method and n points. Then, by recomputing with n/2 points and using richardson method, display the exact error and approximate error. (Test with a=0 b=pi f(x)=sin(x))
1. Write a program that read a sequence of positive integer inputs and print the sum...
1. Write a program that read a sequence of positive integer inputs and print the sum and average of the inputs. Assume that the user always enters a positive number (integer) as an input value or an empty space as a sentinel value. Please use the below to test your code [45 points]: Enter an int value or empty string to end: 98 Enter an int value or empty string to end: 78 Enter an int value or empty string...
Given the data listed in the table, calculate the lower and upper bound for the 95%...
Given the data listed in the table, calculate the lower and upper bound for the 95% confidence interval for the mean at X = 7. The regression equation is given by y^ = b0 + b1x. Regression Statistics Statistic Value b0 4.3 b1 0.50 x 5.36 se 3.116 SSX 25.48 SST 58.25 n 40 Give your answers to 2 decimal places. You may find this Student's t distribution table useful. a) Lower bound = b)Upper bound =
In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and...
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and use as stack to convert the integer to a its corresponding binary representation. Hint: divide the integer by 2. 2. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a...
Write a function printTwoLargest() that inputs an arbitrary number of positive numbers from the user. The...
Write a function printTwoLargest() that inputs an arbitrary number of positive numbers from the user. The input of numbers stops when the first negative or zero value is entered by the user. The function then prints the two largest values entered by the user. If no positive numbers are entered a message to that effect is printed instead of printing any numbers. If only one number is inputted, only the largest is printed out (see 2nd example below). Sample output:...
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT