Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
C++ Program To Find All Factors of A Natural Number
Next article icon

C++ Program for Common Divisors of Two Numbers

Last Updated : 04 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
Given two integer numbers, the task is to find the count of all common divisors of given numbers.
  Input : a = 12, b = 24  Output: 6  // all common divisors are 1, 2, 3,   // 4, 6 and 12    Input : a = 3, b = 17  Output: 1  // all common divisors are 1    Input : a = 20, b = 36  Output: 3  // all common divisors are 1, 2, 4  
CPP
// C++ implementation of program #include <bits/stdc++.h> using namespace std;  // Function to calculate gcd of two numbers int gcd(int a, int b) {     if (a == 0)         return b;     return gcd(b % a, a); }  // Function to calculate all common divisors // of two given numbers // a, b --> input integer numbers int commDiv(int a, int b) {     // find gcd of a, b     int n = gcd(a, b);      // Count divisors of n.     int result = 0;     for (int i = 1; i <= sqrt(n); i++) {         // if 'i' is factor of n         if (n % i == 0) {             // check if divisors are equal             if (n / i == i)                 result += 1;             else                 result += 2;         }     }     return result; }  // Driver program to run the case int main() {     int a = 12, b = 24;     cout << commDiv(a, b);     return 0; } 
Output:
  6  
Please refer complete article on Common Divisors of Two Numbers for more details!

Next Article
C++ Program To Find All Factors of A Natural Number
author
kartik
Improve
Article Tags :
  • Mathematical
  • C++ Programs
  • DSA
  • GCD-LCM
  • divisors
Practice Tags :
  • Mathematical

Similar Reads

  • Count common prime factors of two numbers
    Given two integer [Tex]A [/Tex]and [Tex]B [/Tex], the task is to find the count of common factors of two numbers where factors are prime.Examples: Input: A = 6, B = 12 Output: 2 2 and 3 are the only common prime divisors of 6 and 12Input: A = 4, B = 8 Output: 1 Naive Approach: Iterate from 1 to min(
    10 min read
  • C++ Program To Find LCM of Two Numbers
    LCM (Least Common Multiple) of two numbers is the smallest number that is divisible by both numbers. For example, the LCM of 15 and 20 is 60, and the LCM of 15 and 25 is 75. In this article, we will learn to write a C++ program to find the LCM of two numbers. We can find the LCM of two numbers in C+
    4 min read
  • Divide the two given numbers by their common divisors
    Given two numbers A and B, the task is to Divide the two numbers A and B by their common divisors. The numbers A and B is less than 10^8.Examples: Input: A = 10, B = 15 Output: A = 2, B = 3 The common factors are 1, 5 Input: A = 100, B = 150 Output: A = 2, B = 3 Naive Approach: Iterate from i = 1 to
    10 min read
  • C++ Program To Find All Factors of A Natural Number
    Given a natural number n, print all distinct divisors of it. Examples: Input : n = 10 Output: 1 2 5 10 Input: n = 100 Output: 1 2 4 5 10 20 25 50 100 Input: n = 125 Output: 1 5 25 125 Note that this problem is different from finding all prime factors. Recommended PracticeCount Numbers in RangeTry It
    3 min read
  • C++ Program for GCD of more than two (or array) numbers
    The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) C/C++ Code // C++ program to find GCD of two o
    3 min read
  • C++ Program to 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 Let p1, p2, … pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respect
    3 min read
  • GCD of Two Numbers in C++
    GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that exactly divides both numbers. In this article, we will learn to write a C++ program to find the GCD of two numbers. Example: Input: a = 12, b = 16Output: 4Explanation: As 4 is the largest number wh
    3 min read
  • C++ Program for Smallest K digit number divisible by X
    Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples: Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10 An efficient solution would be : Compute MIN : smallest K-digit num
    2 min read
  • C++ Program to Check Prime Number
    A Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples: Input: n = 29Output: 29 is primeExplanation: 29 has no divisors other than 1 and 29 itself. Hence, it is a prime number. Input: n = 15Output: 15 is NOT primeExplanation: 15 has divisors
    4 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++. Examples Input: n = 11Output: OddExplanation: Since 11 is not completely divisible by 2, it is an odd number.
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences