assignment-s-19.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 22 Dec 2025
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Write a program to calculate the GCD of two numbers (i) using recursion (ii) without recursion
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
int gcd_tail_rec(int, int);
int gcd_rec(int, int);
int gcd_ite(int, int);
int main()
{
int a, b;
printf("Enter two number: ");
scanf("%d %d", &a, &b);
if (a < 0)
a = -a;
if (b < 0)
b = -b;
printf("\nGCD (Tail-Recursion) of %d and %d is = %d", a, b, gcd_tail_rec(a, b));
printf("\nGCD (Recursion) of %d and %d is = %d", a, b, gcd_rec(a, b));
printf("\nGCD (Iteration) of %d and %d is = %d", a, b, gcd_ite(a, b));
return 0;
}
int gcd_tail_rec(int a, int b)
{
if (b == 0)
{
return a;
}
else
{
return gcd_tail_rec(b, a % b);
}
}
int gcd_rec(int a, int b)
{
if (b == 0)
{
return a;
}
else
{
return gcd_rec(b, a % b);
}
}
int gcd_ite(int a, int b)
{
int temp;
while (b > 0)
{
temp = b;
b = a % b;
a = temp;
}
return a;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56