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
#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;
}
Comments
Leave a comment