Question

8. It is possible to check if an integer is divisible by 9 by summing its...

8. It is possible to check if an integer is divisible by 9 by summing its digits: if the digits add up to 9 the number is divisible by 9. For integers up to 90 only a single application of the rule is required. For larger integers, multiple applications may be required. For example, for 99, the digit sum is 18. Then, because 18 still has multiple digits we sum them again to get 9, confirming that 99 is divisible by 9 (after two steps). In general, for a given starting number, you should repeatedly sum the digits until the result is a single digit number. Write a MATLAB script m-file to read in a single number from the keyboard, and use the rule to determine if it is divisible by 9. An appropriate message should be displayed depending on the result. You can assume that the number will always be less than 10,000. [Hints: • To extract the digits of a number use the function ch=num2str(x,'%d'); ch will be an array containing the digits as characters • To convert a character to a number use x1=str2num(ch1)

Homework Answers

Answer #1

Ans

code:-

num=input('Enter number');%input no

ch = num2str(num,'%d');%to array

s=0;%to store sum

while length(ch)~=1 %while digit>1

for i=1:length(ch) %for each digit

x1=ch(i);%

s=s+str2num(x1);%add to s

end

ch=num2str(s,'%d');%to string

s=0;

end

%if digit is 0 or 9 print

if (str2num(ch)==0||str2num(ch)==9)

fprintf('Divisible by 9\n');

else %if not then print

fprintf('Not divisible by 9');

end

.

.

.

If any doubt ask in the comments.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
This question is broken into 3 parts, each of which builds on the functions developed in...
This question is broken into 3 parts, each of which builds on the functions developed in the previous part. Note that you can (and should) still answer parts (b) and (c) even if you cannot answer the preceding parts - just assume that the functions work as they should, and continue. Please explain the code as well Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The...
What would the pseudocode be for this? The code for this has already been answered. I...
What would the pseudocode be for this? The code for this has already been answered. I need the pseudocode. Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375 4657 is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning...
One way to represent a very large integer (one that won't fit into a variable of...
One way to represent a very large integer (one that won't fit into a variable of type short, int, or even long) is to use an array. The array is of type int, so each element in the array can hold an integer -- we will store just one digit of our number per array element. So if a user entered 2375, it might be stored as -------------------------- | 2 | 3 | 7 | 5 | ... | --------------------------...
Do the following problems. 1. Each of three barrels from a manufacturing line are classified as...
Do the following problems. 1. Each of three barrels from a manufacturing line are classified as either above (a) or below (b) the target weight. Provide the ordered sample space. 2. The heat on each of two soldered parts is measured and labeled as either low (l), medium (m), or high (h). State the number of elements in the ordered sample space. 3. Consider the set of Beatles songs with a primary writer as either Paul McCartney (P) or John...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described itself as "a quarterly survey of business leaders from across the globe … surveying 11,500 businesses in 40 economies across the globe on an annual basis." 1 According to the 2011 IBR, the Asia Pacific region had a higher percentage (27 percent) of female chief executive officers (CEOs) than Europe and North America. Japan is the only Asia Pacific region exception. The report further...