Answer to Question #206936 in C++ for kyle

Question #206936

Modify the codes of the given output that will use a function on sum using a function with return type and with parameter.


Use this to view the picture: https://drive.google.com/file/d/1dkhqu95SBESide89FVr9yMUO6x-M7vrm/view?usp=sharing


1
Expert's answer
2021-06-15T01:28:47-0400
#include <iostream>

int sum(int n) {
    int res = 0;
    while (n) {
        res += n % 10;
        n /= 10;
    }
    return res;
}

int main() {
    std::cout << "Enter a positive integer: ";
    int n;
    std::cin >> n;
    int m = sum(n);
    while (true) {
        std::cout << std::endl;
        std::cout << "The sum of the digits = " << m << std::endl;
        int a = m % 3, b = m % 9;
        std::cout << n;
        std::cout << (a ? " is not divisible by 3 " : " is divisible by 3");
        std::cout << (a == 0 and b == 0 or a != 0 and b != 0 ? " and" : ", but");
        std::cout << (b ? " not 9 " : " 9") << std::endl;
        std::cout << std::endl;
        std::cout << "Again: ";
        std::string s;
        std::cin >> s;
        if (s != "y") break;
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog