Question

Write in C++ a function int sumofdigits( int n ) which computes and returns the sum...

Write in C++ a function int sumofdigits( int n ) which computes and returns the sum of the digits of
n.
Use the following main function to test your code:
int main()
{
int n, sn;
cout << "Enter q to quit or an integer: ";
while ( cin >> n )
{
sn = sumofdigits(n);
cout << "sumofdigits( " << n << " ) = " << sn;
cout << "\nEnter q to quit or an integer: ";
}
return 0;
}

Sample output:
Enter q to quit or an integer: 0
sumofdigits( 0 ) = 0
Enter q to quit or an integer: 23
sumofdigits( 23 ) = 5
Enter q to quit or an integer: -345
sumofdigits( -345 ) = 12
Enter q to quit or an integer: 100
sumofdigits( 100 ) = 1
Enter q to quit or an integer: 4
sumofdigits( 4 ) = 4
Enter q to quit or an integer: -345216
sumofdigits( -345216 ) = 21
Enter q to quit or an integer: q

Note: It must compile on its own and have the functions with the correct types and names.

Homework Answers

Answer #1
#include <iostream>

using namespace std;

int sumofdigits(int n) {
    if (n == 0)
        return 0;
    else if (n < 0)
        return sumofdigits(-1*n);
    else
        return (n % 10) + sumofdigits(n/10);
}

int main() {
    int n, sn;
    cout << "Enter q to quit or an integer: ";
    while (cin >> n) {
        sn = sumofdigits(n);
        cout << "sumofdigits( " << n << " ) = " << sn;
        cout << "\nEnter q to quit or an integer: ";
    }
    return 0;
}
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
Using C++, change the formatting in this code by using set precision and set width. The...
Using C++, change the formatting in this code by using set precision and set width. The decimals should line up. #include using namespace std; int main() { int start, end; int ch=1; do{ cout<<"Enter starting and ending range"; cin>>start; cin>>end; if(end36) { cout<<"Invalid range\n"; continue; } cout<<"Inches\tCentimeters\n"; cout<<"******\t***********\n"; for(int i=start;i<=end;i=i+6){ cout< } cout<<"Do you want to continue -1 to quit"; cin>>ch; }while(ch!=-1); return 0; }
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
Please complete the following function getMax, which accepts three integer parameters (int p, int q, int...
Please complete the following function getMax, which accepts three integer parameters (int p, int q, int r), and must return the maximum of those 3 integers received to the caller. int getMax(int p, int q, int r) // returns the max of 3 integers { // begin getMax() // Enter your code here:::::::::::::::::::::::::::::      } // end getMax() void main( ) // To test function getMax( ) { cout << "Data: 9 18 4 , Max = " << getMax(9,...
Define a function SetHeight, with int parameters feetVal and inchesVal, that returns a struct of type...
Define a function SetHeight, with int parameters feetVal and inchesVal, that returns a struct of type WidthFtIn. The function should assign WidthFtIn's data member numFeet with feetVal and numInches with inchesVal. #include <iostream> using namespace std; struct WidthFtIn {    int numFeet;    int numInches; }; /* Your code goes here */ int main() {    WidthFtIn objectSize;    int feetVal;    int inchesVal;    cin >> feetVal >> inchesVal;    objectSize = SetHeight(feetVal, inchesVal);    cout << "Size: "...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
Write a function name as 'square' that returns the square of input value. For example, the...
Write a function name as 'square' that returns the square of input value. For example, the main function is given: int main(){     cout << "The square of " << n << " is " << square(n) << endl;     return 0; } In c++ simple code.
Write a C++ program which consists of several functions besides the main() function. 1)   The main()...
Write a C++ program which consists of several functions besides the main() function. 1)   The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. 2)   A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. 3)   A value-returning function called Power(int a,...