/*I have coded this but it is not working fine it kept saying "Segmentation fault" and i can't track it  down, can someone help me on this i want it badly!*/
#include<iostream>
using namespace std;
int k=0;
void gcd(int a, int b);
void gcd(int a, int b)
{
	static int temp=b;
	int buffer[20],buffer1[20],j,i;
	
	for(i=0; i<temp; i++)
	{
		if(b>0&&temp%b==0)
		{	
		buffer[k]=b;
		k++;
		gcd(a,b-1);
		}
		else
		gcd(a,b-1);
	}
	for(i=0; i<temp; i++)
	{
	if(a%buffer[i]==0)
		buffer1[i]=buffer[i];
	}
	int GCD = buffer1[0];
	for(i=0; i<temp; i++)
	{
		if(buffer1[i]>GCD)
			GCD=buffer1[i];
	}
	cout<<"This is just the gcd"<<GCD;
	
}
int main()
{
	int num,num2;
	cout<<"Enter the first number\n";
	cin>>num;
	cout<<"Enter the second number\n";
	cin>>num2;
	gcd(num,num2);
return 0;
}

Usually when I work on a function I make it in main so If I get a error like a seg fault I take out bit and peices or rebuild the function test each small part and compile and run no error build on and repeat so I can narrow down my search of what exactly being not account for when the seg fault occurs or error. I know I am not helping your problem exactly but I am trying to give insight of how working on a function in main first testing each peice and running it checking for errors can be efficient when debugging and then when error free make it a function and pass it the correct params.

I think the bottom line is that you need some kind of "base case" for your recursion. See this page:
http://www.java-tips.org/java-se-tips/java.lang/finding-greatest-common-divisor-recursively.html
(the language is Java, but the concept is identical)

if (b==0) 
     return a;
else
     return gcd(b, a % b);

The b==0 is their base case, like f(0) = a

Also, along with the above, you should get rid of the for loop as you are letting the recursion go through your values rather than iterating over them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.