Reputation: 23
Today I was asked a C question in a company where i have to find the sum of all divisors of elements of an array individually except itself and store each sum in the same array.For example if array consists of {10,4,6}.Then for 10,it should store 1+2+5=8 in place of 10,then for 4,1+2=3 and so on.I was given 4 variables only():- array[],i,temp,n(size).I could do it using two loops but it requires one variable.Can anyone suggest me the solution?
Upvotes: 1
Views: 382
Reputation: 385590
Since you said you're given the array size as a variable n
, feel free to use it as a loop counter:
while (n > 0) {
--n;
temp = 0; // the sum
for (i = a[n] - 1; i > 0; --i) {
if (a[n] % i == 0) {
temp += i;
}
}
a[n] = temp;
}
Upvotes: 3