Given **sum** and **gcd** of two numbers and . The task is to find both the numbers *a and b*. If the numbers do not exist then print .

**Examples:**

Input:sum = 6, gcd = 2

Output:a = 4, b = 2

4 + 2 = 6 and GCD(4, 2) = 2

Input:sum = 7, gcd = 2

Output:-1

There are no such numbers whose sum is 7 and GCD is 2

**Approach:** As GCD is given then it is known that both the numbers will be multiples of it.

- Choose the first number as
*gcd*then the other number will be*sum – gcd*. - If the sum of both the numbers chosen in the previous step equals to
*sum*then print both the numbers. - Else the numbers do not exist and print
*-1*instead.

Below is the implementation of the above approach:

## C++

`// C++ program to find two numbers ` `// whose sum and GCD is given ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find two numbers ` `// whose sum and gcd is given ` `void` `findTwoNumbers(` `int` `sum, ` `int` `gcd) ` `{ ` ` ` `// sum != gcd checks that both the ` ` ` `// numbers are positive or not ` ` ` `if` `(__gcd(gcd, sum - gcd) == gcd && sum != gcd) ` ` ` `cout << ` `"a = "` `<< min(gcd, sum - gcd) ` ` ` `<< ` `", b = "` `<< sum - min(gcd, sum - gcd) ` ` ` `<< endl; ` ` ` `else` ` ` `cout << -1 << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `sum = 8; ` ` ` `int` `gcd = 2; ` ` ` ` ` `findTwoNumbers(sum, gcd); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find two numbers ` `// whose sum and GCD is given ` `import` `java.util.*; ` `class` `Solution{ ` ` ` `//function to find gcd of two numbers ` `static` `int` `__gcd(` `int` `a,` `int` `b) ` `{ ` ` ` `if` `(b==` `0` `) ` `return` `a; ` ` ` `return` `__gcd(b,a%b); ` `} ` ` ` `// Function to find two numbers ` `// whose sum and gcd is given ` `static` `void` `findTwoNumbers(` `int` `sum, ` `int` `gcd) ` `{ ` ` ` `// sum != gcd checks that both the ` ` ` `// numbers are positive or not ` ` ` `if` `(__gcd(gcd, sum - gcd) == gcd && sum != gcd) ` ` ` `System.out.println( ` `"a = "` `+ Math.min(gcd, sum - gcd) ` ` ` `+ ` `", b = "` `+ (` `int` `)(sum - Math.min(gcd, sum - gcd)) ); ` ` ` `else` ` ` `System.out.println( -` `1` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `sum = ` `8` `; ` ` ` `int` `gcd = ` `2` `; ` ` ` ` ` `findTwoNumbers(sum, gcd); ` ` ` `} ` ` ` ` ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find two numbers ` `# whose sum and GCD is given ` `from` `math ` `import` `gcd as __gcd ` ` ` `# Function to find two numbers ` `# whose sum and gcd is given ` `def` `findTwoNumbers(` `sum` `, gcd): ` ` ` ` ` `# sum != gcd checks that both the ` ` ` `# numbers are positive or not ` ` ` `if` `(__gcd(gcd, ` `sum` `-` `gcd) ` `=` `=` `gcd ` `and` ` ` `sum` `!` `=` `gcd): ` ` ` `print` `(` `"a ="` `, ` `min` `(gcd, ` `sum` `-` `gcd), ` ` ` `", b ="` `, ` `sum` `-` `min` `(gcd, ` `sum` `-` `gcd)) ` ` ` `else` `: ` ` ` `print` `(` `-` `1` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `sum` `=` `8` ` ` `gcd ` `=` `2` ` ` ` ` `findTwoNumbers(` `sum` `, gcd) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find two numbers ` `// whose sum and GCD is given ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// function to find gcd of two numbers ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `__gcd(b, a % b); ` `} ` ` ` `// Function to find two numbers ` `// whose sum and gcd is given ` `static` `void` `findTwoNumbers(` `int` `sum, ` `int` `gcd) ` `{ ` ` ` `// sum != gcd checks that both the ` ` ` `// numbers are positive or not ` ` ` `if` `(__gcd(gcd, sum - gcd) == gcd && sum != gcd) ` ` ` `Console.WriteLine(` `"a = "` `+ Math.Min(gcd, sum - gcd) + ` ` ` `", b = "` `+ (` `int` `)(sum - Math.Min(gcd, sum - gcd))); ` ` ` `else` ` ` `Console.WriteLine( -1 ); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `sum = 8; ` ` ` `int` `gcd = 2; ` ` ` ` ` `findTwoNumbers(sum, gcd); ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find two numbers ` `// whose sum and GCD is given ` ` ` `// Function to find gcd of two numbers ` `function` `__gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` ` ` `return` `__gcd(` `$b` `, ` `$a` `% ` `$b` `); ` `} ` ` ` `// Function to find two numbers ` `// whose sum and gcd is given ` `function` `findTwoNumbers(` `$sum` `, ` `$gcd` `) ` `{ ` ` ` `// sum != gcd checks that both the ` ` ` `// numbers are positive or not ` ` ` `if` `(__gcd(` `$gcd` `, ` `$sum` `- ` `$gcd` `) == ` `$gcd` `&& ` ` ` `$sum` `!= ` `$gcd` `) ` ` ` `echo` `"a = "` `, min(` `$gcd` `, ` `$sum` `- ` `$gcd` `), ` ` ` `" b = "` `, (int)(` `$sum` `- min(` `$gcd` `, ` ` ` `$sum` `- ` `$gcd` `)); ` ` ` `else` ` ` `echo` `(-1); ` `} ` ` ` `// Driver code ` `$sum` `= 8; ` `$gcd` `= 2; ` ` ` `findTwoNumbers(` `$sum` `, ` `$gcd` `); ` ` ` `// This code is contributed by Sachin ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

a = 2, b = 6

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Find two numbers whose sum is N and does not contain any digit as K
- Program to find GCD or HCF of two numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure
- GCD of two numbers formed by n repeating x and y times
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Find the GCD of N Fibonacci Numbers with given Indices
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- GCD of two numbers when one of them can be very large
- GCD of more than two (or array) numbers
- Finding LCM of more than two (or array) numbers without using GCD
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
- GCD of factorials of two numbers
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Find two numbers whose difference of fourth power is equal to N
- Maximum sum of two elements whose digit sum is equal
- Sum of GCD of all numbers upto N with N itself

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.