C++ Program to find sum of even factors of a number Last Updated : 03 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Given a number n, the task is to find the even factor sum of a number. Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Let p1, p2, … pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (p1a1)*(p2a2)* … (pkak). Sum of divisors = (1 + p1 + p12 ... p1a1) * (1 + p2 + p22 ... p2a2) * ........................... (1 + pk + pk2 ... pkak) If number is odd, then there are no even factors, so we simply return 0. If number is even, we use above formula. We only need to ignore 20. All other terms multiply to produce even factor sum. For example, consider n = 18. It can be written as 2132 and sum of all factors is (20 + 21)*(30 + 31 + 32). if we remove 20 then we get the Sum of even factors (2)*(1+3+32) = 26. To remove odd number in even factor, we ignore then 20 which is 1. After this step, we only get even factors. Note that 2 is the only even prime. CPP // Formula based CPP program to find sum of all // divisors of n. #include <bits/stdc++.h> using namespace std; // Returns sum of all factors of n. int sumofFactors(int n) { // If n is odd, then there are no even factors. if (n % 2 != 0) return 0; // Traversing through all prime factors. int res = 1; for (int i = 2; i <= sqrt(n); i++) { // While i divides n, print i and divide n int count = 0, curr_sum = 1, curr_term = 1; while (n % i == 0) { count++; n = n / i; // here we remove the 2^0 that is 1. All // other factors if (i == 2 && count == 1) curr_sum = 0; 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); return res; } // Driver code int main() { int n = 18; cout << sumofFactors(n); return 0; } Output:26 Please refer complete article on Find sum of even factors of a number for more details! Comment More infoAdvertise with us Next Article C++ Program to Print Even Numbers in an Array K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads C++ program to print all Even and Odd numbers from 1 to N Given a number N, the task is to print N even numbers and N odd numbers from 1. Examples: Input: N = 5 Output: Even: 2 4 6 8 10 Odd: 1 3 5 7 9 Input: N = 3 Output: Even: 2 4 6 Odd: 1 3 5 Approach: For Even numbers:Even numbers are numbers that are divisible by 2.To print even numbers from 1 to N, tr 4 min read Program for n-th even number Given a number n, print the nth even number. The 1st even number is 2, 2nd is 4 and so on. Examples: Input : 3Output : 6First three even numbers are 2, 4, 6, .. Input : 5Output : 10First five even numbers are 2, 4, 6, 8, 19.. The nth even number is given by the formula 2*n. C++ // CPP program to fin 2 min read C++ Program to Print Even Numbers in an Array Given an array of numbers, the task is to print all the even elements of the array. Let us understand it with few examples mentioned below. Example: Input: num1 = [2,7,6,5,4] Output: 2, 6, 4 Input: num2 = [1,4,7,8,3] Output: 4, 81. Using for loop Approach: Iterate each element in the given using for 5 min read C++ Program to Check Whether Number is Even or Odd A number is even if it is completely divisible by 2 and it is odd if it is not completely divisible by 2. In this article, we will learn how to check whether a number is even or odd in C++.ExamplesInput: n = 11Output: OddExplanation: Since 11 is not completely divisible by 2, it is an odd number.Inp 3 min read Check if K distinct array elements form an odd sum Given an array A[] of size N, the task is to check if it is possible to get odd sum using K distinct elements from the array.Examples: Input: N = 4, K = 2, A = {10, 19, 14, 14} Output: YES Explanation: 19 + 14 = 33, which is odd Input: N = 3, K = 3, A = {101, 102, 103} Output: NO Explanation: 101 + 7 min read Find sum of even factors of a number Given a number n, the task is to find the even factor sum of a number.Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Recommended PracticeFind sum of even factors of a numberTry It! Prerequisite : Sum of factorsAs discu 8 min read Like