Recursion and Command line Arguments:

1.SWAPPING USING RECURSION:

#include<stdio.h> 
void swap(int*,int*);
int main()
{
int x, y;
   scanf("%d%d",&x,&y);
   swap(&x, &y);
   printf("%d\n%d", x, y);
  return 0;
}
void swap(int *a, int *b)
{
   int temp;
   temp = *a; 
   *a = *b;     
   *b = temp;   

}

2.SUM OF NATURAL NUMBERS USING RECURSION:

#include <stdio.h>
int sum(int );
int main(int argc, char *argv[])
{
  int i;
  i=atoi(argv[1]);
  printf("%d",sum(i));
  return 0;
}
int sum(int n)
{
 int i;
 if(n==0)
   return n;
  else
    return n+sum(n-1);
}

3.FACTORIAL USING RECURSION:

#include <stdio.h>
long int multiplyNumbers(int n);

int main()
{
    int n;
    scanf("%d", &n);
    printf("%ld", multiplyNumbers(n));
    return 0;
}
long int multiplyNumbers(int n)
{
    if (n >= 1)
        return n*multiplyNumbers(n-1);
    else
        return 1;
}

4.SUM OF TWO NUMBERS USING COMMAND LINE ARGUMENTS:

#include <stdio.h>
 
int count, s = 0;
void sum(int *, int *);
 
void main(int argc, char *argv[])
{
    int i, ar[argc];
    count = argc;
    for (i = 1; i < argc; i++)
    {
        ar[i - 1] = atoi(argv[i]);
    }
    sum(ar, ar + 1);
    printf("%d", s);
}
void sum(int  *a, int  * b)
{
    if (count == 1)
        return;
    s = s + *a + *b;
    count -= 2;
    sum(a + 2, b + 2);
}

Comments