in Programming edited by
1,272 views
3 votes
3 votes

Consider the following C functions:

int f1(int a, int b)
{
	while (a != b)
	{
		if(a > b)
			a = a - b;
		else
			b = b - a;
	}
	return a;
}
int f2(int a, int b)
{
	while (b != 0)
	{
		int t = b;
		b = a % b;
		a = t;
	}
	return a;
}
int f3(int a, int b)
{
	while (a != 0)
	{
		int t = a;
		a = b % a;
		b = t;
	}
	return b;
}
  1. All 3 functions return same value for all positive inputs
  2. f1 and f2 return same value for all positive inputs but not f3
  3. For some positive input all 3 functions return different values
  4. f2 and f3 return same value for all positive inputs but not f1
in Programming edited by
by
1.3k views

3 Answers

6 votes
6 votes
Best answer
All three functions are same.
Function calculates GCD of numbers 'a' and 'b'.
selected by

4 Comments

Thanku so much sir ! for replying

input i taken a=5 & b=2
0
0

I just ran it 

[email protected]:~$ ./a.out 5 2
f1: 1
f2: 1
f3: 1

 

0
0
a=5 b=2

2!=0 [right]

t=2

b=a%b i.e 2

a=2

return 2

where i am wrong please correct me!
0
0
0 votes
0 votes
all 3 function are same
0 votes
0 votes

1.) gcd(a,b)

2.) gcd(b,a) ≈ gcd(a,b)

3.) gcd(a,b)

Answer:

Related questions