Question

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 receive

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include<iostream>

#include<cstdlib>

using namespace std;

//method to find the sum of digits of a positive number

//EDIT: now the implemetation works for number with 5 digits only.

int SumOfDigits(int n){

                //initializing sum to 0

                int sum=0;

                //looping for 5 times

                for(int i=0;i<5;i++){

                                //n%10 returns the last digit of n

                                int digit=n%10;

                                //adding to sum

                                sum+=digit;

                                //n/10 removes the last digit from n

                                n=n/10;

                }

                return sum;

}

//method to print the ArmStrong numbers under 500

void ArmStrong(){

                cout<<"Armstrong numbers between 0 and 500"<<endl;

                //since Armstrong numbers mentioned here are of 3 digits, we only need to

                //iterate through every 3 digit numbers upto 500

                for(int i=100;i<=500;i++){

                                //storing i in n

                                int n=i;

                                //extracting last digit

                                int d1=n%10;

                                //removing from n

                                n=n/10;

                                //extracting and removing current last digit

                                int d2=n%10;

                                n=n/10;

                                //extracting current last digit

                                int d3=n%10;

                                //checking if sum of cubes of all three digits is i

                                if((d1*d1*d1)+(d2*d2*d2)+(d3*d3*d3) == i){

                                                //Armstrong number

                                                cout<<i<<endl;

                                }

                }

}

int main(){

                int ch=0;

                //looping

                while(ch!=3){

                                //printing menu, reading choice

                                cout<<"1. To use the SumOfDigits function."<<endl;

                                cout<<"2. To use the ArmStrong function."<<endl;

                                cout<<"3. Exit"<<endl;

                                cin>>ch;

                                //handling choice

                                if(ch==1){

                                                //asking, reading a number, printing sum of digits

                                                int number;

                                                cout<<"Enter a number: ";

                                                cin>>number;

                                                //looping as long as number is not of 5 digits

                                                while(number<10000 || number>99999){

                                                                //asking and reading again

                                                                cout<<"Please enter a valid 5-digit positive number only: ";

                                                                cin>>number;

                                                }

                                                cout<<"Sum of digits: "<<SumOfDigits(number)<<endl;

                                }else if(ch==2){

                                                ArmStrong();

                                }else if(ch==3){

                                                exit(0);

                                }else{

                                                cout<<"Invalid choice!"<<endl;

                                }

                }

                return 0;

}

/*OUTPUT*/

1. To use the SumOfDigits function.

2. To use the ArmStrong function.

3. Exit

1

Enter a number: 12345

Sum of digits: 15

1. To use the SumOfDigits function.

2. To use the ArmStrong function.

3. Exit

1

Enter a number: 55600

Sum of digits: 16

1. To use the SumOfDigits function.

2. To use the ArmStrong function.

3. Exit

2

Armstrong numbers between 0 and 500

153

370

371

407

1. To use the SumOfDigits function.

2. To use the ArmStrong function.

3. Exit

3

Add a comment
Know the answer?
Add Answer to:
Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to...
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
  • To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum f...

    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 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...

  • Programming Fundamental (C++) Lab C++ Lab Ra'fat Amareh Faculty of Engineering and Information Technology Assignment Write...

    Programming Fundamental (C++) Lab C++ Lab Ra'fat Amareh Faculty of Engineering and Information Technology Assignment Write a C++ program that performs the following: Prints on screen a selection menu as follow: a. Sin (x), Series b. Sum odd digit, Print Digits, Print Shape c. Array d. Exit If user presses a or A, the following sub menu should be displayed 1- Sin (x) (Program should solve the following series x - x'/3! + x®/5! – x/7...x"m!) 2- F (Program should...

  • IN PYTHON Write a function called printDigits() that requests the user to input a four-digit integer...

    IN PYTHON Write a function called printDigits() that requests the user to input a four-digit integer and prints the digits using math function, as shown below. You are not allowed to process the number as a string. You must process the number using standard arithmetic operators (+, *, /, %, etc.) >>> printDigits() Enter n: 1234 1 2 3 4 >>> printDigits() Enter n: 9876 9 8 7 6 >>>

  • Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer...

    Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. Draw the flowchart and write a Java code to find ALL Armstrong number in the range of 0 and 999. You should write your code in two ways: (Yes as if you are...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • Please Use Python and provide explanations!!!! 1. Write a function called multiply_and_print that takes two numbers,...

    Please Use Python and provide explanations!!!! 1. Write a function called multiply_and_print that takes two numbers, multiplies them together, and prints out the sentence '____ times ____ is ____'. Ask the user for two numbers, and call multiply_and_print with their two numbers as arguments. 2.Write a function called roll_dice that rolls two six-sided dice and prints out the result of each die. Call it three times. (Remember, you can import the random module with the statement import random, and then...

  • 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

  • Please use python to write the simple program. Exercise 2 - Summation Write a program to...

    Please use python to write the simple program. Exercise 2 - Summation Write a program to accept integer inputs until the user inputs a non-integer. Then, the program prints the summation of the inputted numbers. Hint: Use the function input() to get the user input. Use a variable total for calculating the summation. Need to use while loop for the repeated inputs. Use if statement with isdigit() function to check whether the user input is an integer or not. if...

  • MATLAB QUESTION Write a MATLAB function called vector_input that takes no inputs and returns no outputs. Instead, when c...

    MATLAB QUESTION Write a MATLAB function called vector_input that takes no inputs and returns no outputs. Instead, when called, it gives control to the user and asks the user to input a length-3 vector. Then the function prints: The sum of ___, ____, and ____ is ____. [new line] where the first three blanks are filled by each element in input vector, and the last blank is the sum of all three elements.

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