Question
use matlab
To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum function, enter3 To exit the
Write a function, called digits function that is able to calculate the number of digits and the summation of the digits. The
To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum function, enter3 To exit the program, enter 4 Please select a number = 6 Please re-select again: 2 please enter the first number 3 please enter the second number: 6 please enter the third number: 3 The average equals to: 4
Write a function, called digits function that is able to calculate the number of digits and the summation of the digits. The input of this function is N (entered number) and the outputs are number_digits and sum digits. (4 marks) Write a function, called print average function that is able to calculate the average of 3 numbers and pri. The inputs of this function are a, b and c. No output is required. (3 marks) e Write a function, called Perfect sum function that receives three digit number, and checks whether the number is Armstrong number or not. The in and the output is perfect number. (4 marks) put of this function is n Example of Perfect Sum number: Now after you created the three functions, you have to write a program that can call the above functions according to the user's choice. Write a menu driven that allows the user to select from the following options (4 marks) I. To use the digits function you have to enter 1 2 To use the average function you have to enter 2. To use the perfect sum function you have to enter 3. .To Exit the program you have to enter 4 Once the user select one of the above choices you have to read the value entered by the user and then to call the respective function. If the user entered a number that is not equal to 1 or 2 or 3 or 4, the program should ask the user to re-enter again. Then, you have to use "input" to enter the values of the variables (inputs of the functions). For the digits function, you have to enter only positive number. For the average function, you have to enter greater than or equal to zero values and for the perfect sum function you have to enter a three digit number For all of the functions, the program should ask the user to re-enter again if the user entered wrong numbers
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Using Matlab

Main Function : -

clc;
clear all;
flag=1;
while(flag)
disp('For DIGITS COUNT function enter 1');
disp('For AVERAGE function enter 2');
disp('For PRIME function enter 3');
disp('To EXIT enter 4');
prompt = 'your choice: ';
choice = input(prompt);
switch (choice)


case 1


prompt = 'Enter a positive number to calculate digits and sum: ';
N = input(prompt);
nd=numel(num2str(N)); %numel is used focalulating number of element in string and num2str convert number to string
  
while(N<0 || nd<5)
if(N<0)
prompt = 'Number entered should be positive, re-enter number ';
N = input(prompt);
end
if(nd<5)
prompt = 'Number entered should have more than 4 digits, re-enter number ';
N = input(prompt);
end
nd=numel(num2str(N));
end
  
[ number_digits,sum_digits ] = digits_function( N );
fprintf('The number of digits is: = %d\n',number_digits);
fprintf('The sum of digits is: = %d\n\n',sum_digits);
  
case 2


prompt = 'Enter the first input ';
a = input(prompt);
prompt = 'Enter the second input ';
b = input(prompt);
prompt = 'Enter the third input ';
c = input(prompt);
prompt = 'Enter the forth input ';
d = input(prompt);
  
average_function( a,b,c,d);
  
  
case 3


prompt = 'Enter a number to check if it is prime: ';
x = input(prompt);   
while(x<1 || x==1)
prompt = 'Number entered should be greater than 1, re-enter number ';
x = input(prompt);
end
  
[ result_prime ] = prime_function( x );
if(result_prime==1)
fprintf('%d is a prime number\n\n',x);
else
fprintf('%d is not a prime number\n\n',x);
end
case 4
flag=0;
break;
  
otherwise
disp('invalid option, re-enter your option');
fprintf('\n\n');
  
end
  
  

  1. digits_function

function [ number_digits,sum_digits ] = digits_function( N )
nd=numel(num2str(N)); %numel is used focalulating number of element in string and num2str convert number to string
sd=0;
b=num2str(N); %num2str convert number to string;
for i=1:nd
str = str2double(b(i)); %string2double convert stinrg to double so that we can add it
sd=sd+str; %sd=sd+b(i) this we cannot write as the value of b(i) is considered to be ASCII value of int
end
number_digits=nd;
sum_digits=sd;

end

  1. print_average_function

function average_function( a,b,c,d)
sum=a+b+c+d; %first we add all 4 variable then minus then if they fall in fallowing four cases
count=4;
if (a==0 || a>10) %given condition value should not ne 0 or greater than 10
count=count-1;
sum=sum-a;
end
if (b==0 || b>10 )
count=count-1;
sum=sum-b;
end
if (c==0 || c>10)
count=count-1;
sum=sum-c;
end
if (d==0 || d>10)
count=count-1;
sum=sum-d;
end

if(count~=0)
avg=sum/count;

end

fprintf('The average is: %d\n\n',avg);
end

Add a comment
Know the answer?
Add Answer to:
To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum f...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to...

    Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to calculate the summation of a five-digit number. This function receives one integer parameter 'a', then returns the sum of 5 digits of the number. [2.5 marks] b. Write a function, called Armstrong that prints all Armstrong numbers in the range of 0 and 500. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is...

  • (Java Please) Sum of Digits Write a program that will sum the digits of a number...

    (Java Please) Sum of Digits Write a program that will sum the digits of a number input by the user. For example, if the user enters the number 1234, the sum of the digits will be 10 (1 + 2 + 3 + 4 = 10). The user will enter a number with at least 4 digits (greater than 1000). That value will be sent to a method which will return the sum. Sample Output 1: SUM OF DIGITS -------------...

  • Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...

    Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • 2. Write an application that inputs a 3-digits number from the user and checks if all...

    2. Write an application that inputs a 3-digits number from the user and checks if all digits are prime numbers. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers don't use an if to check numbers from 1 - 9; you need to find an algorithm to check if the number is prime or not. Enter a 3-digit...

  • A perfect number is a positive integer that is equal to the sum of its (proper)...

    A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...

  • PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the...

    PYTHON: (Sum the digits in an integer using recursion) Write a recursive function that computes the sum of the digits in an integer. Use the following function header: def sumDigits(n): For example, sumDigits(234) returns 9. Write a test program that prompts the user to enter an integer and displays the sum of its digits. Sample Run Enter an integer: 231498 The sum of digits in 231498 is 27

  • A positive integer is said to be a perfect number if it equals the sum of...

    A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is aperfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try...

  • 3. Write a complete C++ program that finds and prints the sum of values from 1...

    3. Write a complete C++ program that finds and prints the sum of values from 1 to N where N is a positive value > 10 entered by the user. If the user inputs a value less the 10 ask him to re-enter the value again and again until the value is valid. (20 Points)

  • Code in C An integer is divisible by 9 if the sum of its digits is...

    Code in C An integer is divisible by 9 if the sum of its digits is divisibleExample output: by 9. Develop a program which will call UDF: int get input(); to prompt the user for an integer and return this user input to main() Call UDF: Enter an integer: 5463 3 4 5 5463 is divisible by 9 void display int val); to display each digit of the integer starting with the rightmost digit. Your program should also determine whether...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT