There is a program code that finds the sum of the divisors of the number. True, he is not mine and written personally for me is not clear. I tried to write the code (commented out), but nothing happened. Calculates only for odd numbers and that is not always correct. Tell me where I was wrong.
#include <iostream> using namespace std; //int sd(int n, int d=1) //{ // if (d <= n) // { // if(n % d == 0) // return d + sd(n, d + 1); // else // return sd(n, d + 1); // } //} int sd(int n, int d=1) { return d <= n ? (n % d == 0? d + sd(n, d + 1) : sd(n, d + 1)) : 0; } int main() { setlocale(LC_ALL, "rus"); int n; cout<<"Дано натуральное число найти сумму его делителей через рекурсию \n"<<endl; cout << "Введите натуральлное число, n = "; cin >> n; cout <<"Сумма делителей натурального числа = "<<sd(n)<<endl; system ("pause"); return 0; }