Sum of common divisors of two numbers A and B | C ++ implementation of above approach ; print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; print the sum of common factors
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int a , int b ) { int sum = 0 ; for ( int i = 1 ; i <= min ( a , b ) ; i ++ ) if ( a % i == 0 && b % i == 0 ) sum += i ; return sum ; } int main ( ) { int A = 10 , B = 15 ; cout << " Sum ▁ = ▁ " << sum ( A , B ) << endl ; return 0 ; }