Question: TASK MUST BE ADDED TO THE GIVEN PROGRAM BELOW. PLEASE DON'T WRITE THE CODE AS A SEPERATE PROGRAM! PLEASE READ THE TASK CAREFULLY BEFORE YOU
TASK MUST BE ADDED TO THE GIVEN PROGRAM BELOW. PLEASE DON'T WRITE THE CODE AS A SEPERATE PROGRAM!
PLEASE READ THE TASK CAREFULLY BEFORE YOU START. 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 :
A well-known theorem of math states that the above sum is 9 iff the number is divisible by 9. Write a function that modifies its argument number so that its divisible by 9. The function should add something to the right-most digit of the number if possible; otherwise it should subtract something from that digit. An example use of this function might be:
transformNum(n); cout << n; // prints 234567 if n was originally 234565
Write the transformNum function. You should be able to reuse most of the code from earlier. Also modify the driver program to output the transformed number.
// GIVEN PROGRAM 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 number
int getDigit(int num, int index);
// Precondition: num > 0
// Postcondition: the return value is the iterated sum of digits of number
int sumDigits(int num);
int main(){
int n;
cout<<"Enter a number: ";
cin>>n;
cout<<"The 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
