Computer Science, asked by suprajav78, 11 months ago

Write a C++ Program to find the sum of odd factors of the number.(factor of 6 is 1,2,3 and 6 so sum =1+3=4)

Answers

Answered by Arslankincsem
0

#include <bits/stdc++.h>

usingnamespacestd;  

 

// Returns sum of all factors of n.  

intsumofoddFactors(intn)  

{  

   // Traversing through all  

   // prime factors.  

   intres = 1;  

 

   // ignore even factors by  

   // removing all powers of  

   // 2  

   while(n % 2 == 0)  

       n = n / 2;  

 

   for(inti = 3; i <= sqrt(n); i++)  

   {  

 

       // While i divides n, print  

       // i and divide n  

       intcount = 0, curr_sum = 1  

       intcurr_term = 1;  

       while(n % i == 0) {  

           count++;  

 

           n = n / i;  

 

           curr_term *= i;  

           curr_sum += curr_term;  

       }  

 

       res *= curr_sum;  

   }  

 

   // This condition is to handle  

   // the case when n is a prime  

   // number.  

if(n >= 2)  

       res *= (1 + n);  

 

       returnres;  

   }  

       

   // Driver code  

intmain()  

   {  

   intn = 30;  

   cout<<sumofoddFactors(n);  

   return0;  

}  


Similar questions