NetApp Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
#include<stdio.h>
void main()
{
int num1, num2;
int div, rem,quot;
printf("\n Enter number 1 : \n");
scanf("%d",&num1);
printf("\n Enter number 2 : \n");
scanf("%d",&num2);
if(num1<num2)
{
quot=num1;
div=num2;
}
else
{
quot=num2;
div=num1;
}
rem=num1+num2;
while(rem)
{
rem=div%quot;
if(rem!=0)
{
div=quot;
quot=rem;
}
}
printf("\n GCF is is <%d> \n",quot);
}
#include <stdio.h>
int calculategcd(int first,int second)
{
int gcd=1,loop=0,i=0;
if(first ==0 || second ==0 )
return 0;
if(first==1|| second==1)
{
gcd=1;
return gcd;
}
if(first<second)
loop =first;
else
loop=second;
for(i=2;i<=loop;i++)
{
if((first%i==0)&&(second%i==0))
gcd=gcd*i;
}
return gcd;
}
int main()
{
int first =0,second=0,gcd =0;
scanf("%d %d",&first,&second );
gcd =calculategcd(first,second);
printf("GCD =%d",gcd);
getchar();
return 0;
}
The easiest way of finding the GCD of two numbers is by using the Euclidean algorithm.
en.wikipedia.org/wiki/Euclidean_algorithm.
Code:
Recursive Code
Non-Recursive:
- Coder January 28, 2014