HCF (GCD) of long values
The program must accept two long values X, Y and print their HCF (GCD).
Input Format:
The first line contains X and Y separated by a space.
The first line contains X and Y separated by a space.
Output Format:
The first line contains the HCF (GCD) of X and Y.
The first line contains the HCF (GCD) of X and Y.
Boundary Conditions:
1 <= X, Y <= 999999999999
1 <= X, Y <= 999999999999
Example Input/Output 1:
Input:
20 30
Input:
20 30
Output:
10
10
Example Input/Output 2:
Input:
999999999999 151515151515
Input:
999999999999 151515151515
Output:
30303030303
Code:30303030303
C++:
#include
<iostream>
using
namespace std;
int
main(int argc, char** argv)
{
long
a,b;
cin>>a>>b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
cout<<a;
}
Java:
import
java.util.*;
class
Gcd
{
public static void main(String
args[])throws Exception
{
Scanner sc = new
Scanner(System.in);
System.out.print("Enter the
First no : ");
long n1=sc.nextLong();
System.out.print("Enter the
Second no : ");
long n2=sc.nextLong();
while(n1 != n2)
{
if(n1 > n2)
n1 = n1-n2;
else
n2 = n2-n1;
}
System.out.print("GCD =
"+n1);
}
}
Please do comment If u have any Queries!
No comments:
Post a Comment