Number of Integral Points between Two Point

#include<bits/stdc++.h>
using namespace std;

int gcd(int a, int b)
{
if(b == 0)
return a;

return gcd(b, a%b);
}

int pointcount(int x1, int y1, int x2, int y2)
{
int r1 = abs(x1-x2);
int r2 = abs(y1-y2);

if(x1 == x2)
return r1-1;

else if(y1 == y2)
return r2-1;

else
return gcd(r1, r2)-1;
}

int main()
{
int x1, y1, x2, y2;

cout << "Enter x1 and y1 :: ";
cin >> x1 >> y1;

cout << "\nEnter x2 and y2 :: ";
cin >> x2 >> y2;

cout << "\nNumber of integral points between the given two point :: " << pointcount(x1, y1, x2, y2) << "\n";

return 0;
}

Comments