Question

In: Computer Science

Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...

Code needed in C++

(nOT IN STEP BY STEP EITHER)

  

Write a recursive function that computes the sum of the digits in an integer. Use the following function header:

int sumDigits(int n)

For example, sumDigits(234) returns 2 + 3 + 4 = 9.

Write a test program that prompts the user to enter an integer and displays its sum.

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

#include <iostream>
using namespace std;

/*main function*/
int main()
{
   /*function prototype*/
   int sumDigits(int);
   /*variables*/
   int n,sum;
   /*read an integer from user*/
   cout<<"Enter an integer: ";
   cin>>n;
   /*function call*/
   sum=sumDigits(n);
   /*print sum*/
   cout<<"The sum of digits is: "<<sum;
   return 0;
}
/*function definition*/
int sumDigits(int n)
{
   if(n==0)
       return n;
   /*recursive call by adding digits*/
   else
       return (n%10)+sumDigits(n/10);
}


Related Solutions

Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer ·and takes as parameters: (1)   a pointer to double. The pointer is the address of the start of an array, (2)   the “end” pointer to the address after the array (3)   and the address of the smallest value seen so far ·Write main() to test this function – try a case where the array...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
it should be c++ data structure Write a recursive function that draws the following shape. X...
it should be c++ data structure Write a recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX XXX X The length of the longest row in the shape and the shape's character are function parameters. In above shape the longest row is 9 and the pattern's character is "X”.
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT