Question: PLEASE READ THE TASK AND THE DIRECTION CAREFULLY BEFORE YOU START. I NEED THE FULL TASK. IF YOU ARE NOT SURE WHAT TO DO, PLEASE
PLEASE READ THE TASK AND THE DIRECTION CAREFULLY BEFORE YOU START. I NEED THE FULL TASK. IF YOU ARE NOT SURE WHAT TO DO, PLEASE SKIP MY QUESTION. I WILL GIVE NEGATIVE RATING FOR WRONG OR INCOMPLETE ANSWER.
THANK YOU.
TASK 1:
You need a driver program to test program below. To do this, write a main function that prompts for and input num, calls sumdigits , and outputs the result. You may assume that the input is a positive number and do not need to do any error checking (but realize that you would have to for a real program).
DIRECTIONS:
Please write the driver program separate from the main program. I need all work in very clear steps.
// CODE STARTS HERE
#include
using namespace std;
// Precondition: num > 0
// Postcondition: the return number of digits in num
int numDigits(int num);
// Precondition: num > 0, index > 0
// Postcondition: return the index'th digit of num
int getDigit(int num, int index);
// Precondition: num > 0
// Postcondition: the return value is the iterated sum of digits of num
int sumDigits(int num);
int main(){
int n;
cout<<"Enter a integer: ";
cin>>n;
cout<<"Sum of digits = "< return 0; } int numDigits(int num){ int count = 0; while(num > 0){ num = num/10; count++; } return count; } // Precondition: num > 0, index > 0 // Postcondition: return the index'th digit of num int getDigit(int num, int index){ int digit; for(int i=1; i<=index; i++){ digit = num%10; num = num/10; } return digit; } // Precondition: num > 0 // Postcondition: the return value is the iterated sum of digits of num int sumDigits(int num){ int n = numDigits(num); int sum = 0; for(int i=1; i<=n; i++) sum = sum + getDigit(num, i); return sum; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
