In: Computer Science
Write a Diophantine Equation program in C++.
Find integer solutions to Ax + By = GCD (A, B)
Ex:
a = 3, b = 6, c = 9
a = 2, b = 5 , c = 1
#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b) //function for find the GCD of 2 numbers
{
if(a%b==0)
return abs(b); //it returns the absolute Integer value
else
return gcd(b,a%b);
}
bool isPossible(int a,int b,int c) //chekcs if integral solutions are possible
{
if(c%gcd(a,b)==0)
return true;
else
return false;
}
int main()
{
int a,b,c;
cout<<"enter the values of a,b,c:\n"; //taking input
cin>>a;
cin>>b;
cin>>c;
if(isPossible(a,b,c)) //checks possible or not
{
cout<<"possible\n";
}
else
cout<<"not possible\n";
}
Note : abs() - it's a build-in-function, returns the absolute Integer value.
Note : for indentation,pls look at screen shot.