diff --git a/c/1darray.c b/c/1darray.c index c7faaf0..173f8d4 100644 --- a/c/1darray.c +++ b/c/1darray.c @@ -14,4 +14,4 @@ int main(){ printf("Sum=%d",sum); return 0; -} \ No newline at end of file +} diff --git a/c/2darray.c b/c/2darray.c index 0a2bf8b..cb4fcf5 100644 --- a/c/2darray.c +++ b/c/2darray.c @@ -20,4 +20,4 @@ int main(){ } } return 0; -} \ No newline at end of file +} diff --git a/c/a.out b/c/a.out new file mode 100755 index 0000000..e8f984d Binary files /dev/null and b/c/a.out differ diff --git a/c/callByRef.c b/c/callByRef.c index 3115dc1..10955b2 100644 --- a/c/callByRef.c +++ b/c/callByRef.c @@ -32,4 +32,4 @@ void callByRef(int *x,int *y){ *y=*t; printf("From call by value\n"); printf("x=%d\ty=%d\n",*x,*y); -} \ No newline at end of file +} diff --git a/c/cylinder.c b/c/cylinder.c index 55bda34..fdb7375 100644 --- a/c/cylinder.c +++ b/c/cylinder.c @@ -33,4 +33,4 @@ void cylinder(float r, float h, float *csa, float *tsa, float *vol){ *csa = 2*r*h*PI; *tsa = 2*r*(h+r)*PI; *vol = r*r*h*PI; -} \ No newline at end of file +} diff --git a/c/datatype.c b/c/datatype.c index 6813326..0fbc42f 100644 --- a/c/datatype.c +++ b/c/datatype.c @@ -12,4 +12,4 @@ int main(){ //printing data printf("%c %d %f %lf",ch,i,f,d); return 0; -} \ No newline at end of file +} diff --git a/c/empstruct.c b/c/empstruct.c index 9b53542..0ba5cab 100644 --- a/c/empstruct.c +++ b/c/empstruct.c @@ -37,4 +37,4 @@ int main(){ printf("\nHighest Earner Detail\n"); printf("Name: %s\nPost: %s\nSalary: %.2f\n",high.name,high.post,high.salary); return 0; -} \ No newline at end of file +} diff --git a/c/forloop.c b/c/forloop.c index 79bce71..e6feae5 100644 --- a/c/forloop.c +++ b/c/forloop.c @@ -4,6 +4,7 @@ int main(){ int i,n; //reading value of n + printf("Enter n: "); scanf("%d",&n); //for loop @@ -11,4 +12,4 @@ int main(){ printf("%d\n",i); } return 0; -} \ No newline at end of file +} diff --git a/c/hello.c b/c/hello.c index cf363a5..bc71556 100644 --- a/c/hello.c +++ b/c/hello.c @@ -2,4 +2,4 @@ int main(){ printf("Hello World"); return 0; -} \ No newline at end of file +} diff --git a/recursion/a.out b/recursion/a.out new file mode 100755 index 0000000..b54f1c1 Binary files /dev/null and b/recursion/a.out differ diff --git a/recursion/gcd.c b/recursion/gcd.c index cf94b58..77b3f10 100644 --- a/recursion/gcd.c +++ b/recursion/gcd.c @@ -1,8 +1,12 @@ #include + +//recursive function for gcd of two numbers. int gcd(int x, int y); +//iterative function for gcd of two numbers. +int gcd_iter(int u , int v); int main(){ - int a, b, g; + int a, b, g, f; printf("Enter a and b:\n"); scanf("%d%d", &a, &b); @@ -10,17 +14,37 @@ int main(){ //gcd g = gcd(a, b); + //gcd_iter + f=gcd_iter(a, b); + //in case g is negative, then convert it into positive if(g < 0){ g *= -1; } + //in case f is negative, then convert it into positive + if(f < 0){ + f=f*(-1); + } + //output - printf("GCD(%d, %d) = %d\n", a, b, g); + printf("Recursive GCD(%d, %d) = %d\n", a, b, g); + printf("Iterative GCD(%d, %d) = %d\n", a, b, f); return 0; } int gcd(int x, int y){ if(y == 0) return x; else gcd(y, x%y); -} \ No newline at end of file +} + + +int gcd_iter(int u, int v){ + int t; + while (v) { + t = u; + u = v; + v = t % v; + } + return u < 0 ? -u : u; /* abs(u) */ +} diff --git a/recursion/towerOfHanoi.c b/recursion/towerOfHanoi.c index 6274d89..15850e3 100644 --- a/recursion/towerOfHanoi.c +++ b/recursion/towerOfHanoi.c @@ -19,4 +19,4 @@ void t(int n, char beg, char aux, char end){ t(1, beg, aux, end); t(n-1, aux, beg, end); } -}//t() ends here \ No newline at end of file +}//t() ends here