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

  • Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices....

    Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices. 1. Find the number of digits in an integer. 2. Find the nth digit in an integer. 3. Find the sum of all digits of an integer. 4. Is the integer a palindrome? 5. Quit Enter a choice: Page 1 of 4 For each of the choices (1, 3, 4), Read a positive integer number and call a function that processes the menu choice...

  • 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 C++ program to prompt user to enter a 3-digits number, say 371, to a variable...

    Write C++ program to prompt user to enter a 3-digits number, say 371, to a variable called num. Then store each digit of this inputted number to three different variables called digit1, digit2 and digit3. Hence, when number entered is 371, digit1 is 3, digit2 is 7 and digit3 is 1. Display the value of inputted number followed by digit1, digit2 and digit3. Hint: You have to use / and % operator in this question.

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

  • using C++, NOT C language 1. Write a function called insert() to insert a node to...

    using C++, NOT C language 1. Write a function called insert() to insert a node to the beginning of a linked list. The data is passed into the function. For example insert(8) will insert a node at the beginning of the list with the number 8 in the data field. Each node in the linked list is a ListNode struct as discussed in class. 2. Write a function called print() that will traverse the entire linked list and print out...

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