-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgcd.c
More file actions
51 lines (43 loc) · 1.02 KB
/
pgcd.c
File metadata and controls
51 lines (43 loc) · 1.02 KB
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
// Assignment name : pgcd
// Expected files : pgcd.c
// Allowed functions: printf, atoi, malloc, free
// --------------------------------------------------------------------------------
// Write a program that takes two strings representing two strictly positive
// integers that fit in an int.
// Display their highest common denominator followed by a newline (It's always a
// strictly positive integer).
// If the number of parameters is not 2, display a newline.
// Examples:
// $> ./pgcd 42 10 | cat -e
// 2$
// $> ./pgcd 42 12 | cat -e
// 6$
// $> ./pgcd 14 77 | cat -e
// 7$
// $> ./pgcd 17 3 | cat -e
// 1$
// $> ./pgcd | cat -e
// $
#include <stdlib.h>
#include <stdio.h>
int main(int ac, char **av)
{
if (ac == 3)
{
int n1 = atoi(av[1]);
int n2 = atoi(av[2]);
if (n1 > 0 && n2 > 0)
{
while (n1 != n2)
{
if (n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("%d", n1);
}
}
printf("\n");
return 0;
}