## INDEX

### FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM

Find gcd of a number using recursion in c program

#include<stdio.h>
int main(){
int n1,n2,gcd;
printf("\nEnter two numbers: ");
scanf("%d %d",&n1,&n2);
gcd=findgcd(n1,n2);
printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
return 0;
}

int findgcd(int x,int y){
while(x!=y){
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}

Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

1. s it can b made simpler...

int gcd(int m ,int n)
{
if(m==0)
return(n);
if(n==0)
return(m);
return(n,m%n);
}
void main()
{
int m,n;
clrscr();
printf("enter 2 numbers");
scanf("%d%d",&m,&n);
printf("gcd is %d",gcd(m,n));
getch();
}

1. Prashant is correct......instead of writing return gcd(n,m%n) he wrote return (n,m%n)

2. hahahah... try it dude! if u enter 24 & 12 its returning 0... it is not correct

2. prashant is wrong his program is incorrect...........

3. He is correct but he wrote return(n,m%n); instead of gcd(n,m%n); in 7th line of his program

4. I try it now..

5. programm can not run correctly

6. Prashanth your program is correct but it would be better if u had done the program with recursion

7. prrashant do itusing recursion

8. using recursion is not advisable in this prog because depth of recursion will be huge if no supplied by users are big..in that case time complexity of prog will tend to its worst case ..i agree strongly with abhinav's way of coding

9. Brilliant program Prashanth!

10. int gcd(int n, int m)
{
int gcdVal = 0;
if(m>n)
{
gcdVal = gcdRec(m,n,n);
}
else
{
gcdVal = gcdRec(n,m,m);
}

}

int gcdRec(int m, int n, int k)
{
if(m%k==0 && n%k==0)
return k;

gcdRec(m,n,k-1);
}

12. IT'S VERY HEPLFULL TO ME.
NICE GOOD JOB...........

13. plz explain working

14. frnds pls give a crct program fr gcd by using recursion

1. #include
#include
void main()
{
int gcd(int,int);
int a,b;
printf("enter the two numbers");
scanf("%d%d",&a,&b);
printf("gcd is %d",gcd(a,b));
getch();
}
int gcd(int a,int b)
{
if(a%b==0)
{
return b;
}
else
return gcd(b,a%b);
}

2. i think the input of a has to be greater than b in this case. isn't it ?

15. This comment has been removed by the author.

16. #include
int main()
{
int n1,n2,i,min;
printf("enter 2 numbers");
scanf("%d %d", &n1,&n2);

min = n1 < n2 ? n1: n2;
for ( i= min; i>=1; i-- )
{
if(n1%i==0 && n2%i==0)
{
printf("hcf is %d ",i);
break;
}
}
}

17. it can also be written as

#include
void main()
{
int gcd(int,int);
int a,b;
printf("enter a,b values \n");
scanf("%d%d",&a,&b);
printf("gcd of %d,%d is %d",a,b,gcd(a,b));
getch();
}
int gcd(int a,int b)
{
if(a>b)
gcd(b,a);
if(a==0)
return(b);
else
gcd(b%a,a);
}

18. This is a decent program, but it does not work for all cases. Try passing in findgcd(10, 0). It fails!!! Because the functions goes into an infinite recursion that never ends.

19. Guys it would be better if you check for any idiot entering 0 as one of the two numbers, in that case it may print the non zero number as the gcd for some programs or, it could cause an infinite repetition of the loop for some other programs .... its better if you print it as an error as soon as 0 is entered

20. This comment has been removed by the author.

21. it was not getting it is showing gcd of 12 and 8 is 8

22. it was not getting it is showing gcd of 12 and 8 is 8

23. int gcd(int x, int y)
{ return (y==0) ? x : gcd(y, x%y); }

24. This comment has been removed by the author.

25. int findGcd(int a, int b)
{
Static int i=1,gcd=1;
if(i>b)
{
return gcd;
}
if(b%i==0&&a%i==0)
{
gcd=i;
}
findGcd(a,b);
}

26. Try this very simple process in bits vizag
#include
Int gcd(int,int);
main()
{
Int a,b,r;
clrscr();
Print("enter the values of a,b");
Scanf("%d%d",&a,&b);
R=Gcd(a,b);
Print("the gcd value is=%d,r);
}
Int gcd(int x,int y)
{
If(y==0)
return x;
else
gcd(y,y%x);
}

27. Guys, can u please tell me which is the correct prog??

28. Which is wrt yar