More on functions – Theory and Programs [Learn to code]

Learn more theory about Functions. And different programs on functions. What is recursion? And programs on recursion.

  • 063. Write a program to accept two numbers from user and swap their values using call by reference method in C language
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
     
    swap(int *x ,int *y)
    {
    	int temp;
    	temp=*x;
    	*x=*y;
    	*y=temp;
    }
    main()
    {
    	int a,b;
    	clrscr();
    	printf("Enter numbers : ");
    	scanf("%d%d",&a,&b);
            printf("nBefore Swapping a = %d, b = %dn",a,b);
    	swap(&a,&b);
    	printf("nAfter  Swapping a = %d, b = %dn",a,b);
    	getch();
    }
  • 064. Write a program using recursions for fibbonacci series in C language
     
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    fibbo(int pre, int cur ,int x)
    {        int temp;
    	if(x==2)
    	{
    		getch();
    		exit(0);
    	}
    	temp=cur;
    	cur=pre+cur;
    	pre=temp;
    	printf("%d ",cur);
    	fibbo(pre,cur,x-1);
    }
    main()
    {
    	int n,pre=1,cur=1;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("%d %d ",pre,cur);
            fibbo(pre,cur,n);
    	getch();
    }
  • 065. Write a program to print factorial of a given number using recursive function in C language
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
     
    int fact(int n)
    {
    	int f;
    	if(n==1)
    		return 1;
    	else
    		f=n*fact(n-1);
    	return f;
    }
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("nFactorial is...%dn",fact(n));
    	getch();
    }

065. Write a program to print factorial of a given number using recursive function in C language

#include<stdio.h>
#include<conio.h>
#include<process.h>
 
int fact(int n)
{
	int f;
	if(n==1)
		return 1;
	else
		f=n*fact(n-1);
	return f;
}
main()
{
	int n;
	clrscr();
	printf("Enter number : ");
	scanf("%d",&n);
	printf("\nFactorial is...%d\n",fact(n));
	getch();
}

064. Write a program using recursions for fibbonacci series in C language

 
#include<stdio.h>
#include<conio.h>
#include<process.h>
fibbo(int pre, int cur ,int x)
{        int temp;
	if(x==2)
	{
		getch();
		exit(0);
	}
	temp=cur;
	cur=pre+cur;
	pre=temp;
	printf("%d ",cur);
	fibbo(pre,cur,x-1);
}
main()
{
	int n,pre=1,cur=1;
	clrscr();
	printf("Enter number : ");
	scanf("%d",&n);
	printf("%d %d ",pre,cur);
        fibbo(pre,cur,n);
	getch();
}

063. Write a program to accept two numbers from user and swap their values using call by reference method in C language

#include<stdio.h>
#include<conio.h>
#include<process.h>
 
swap(int *x ,int *y)
{
	int temp;
	temp=*x;
	*x=*y;
	*y=temp;
}
main()
{
	int a,b;
	clrscr();
	printf("Enter numbers : ");
	scanf("%d%d",&a,&b);
        printf("\nBefore Swapping a = %d, b = %d\n",a,b);
	swap(&a,&b);
	printf("\nAfter  Swapping a = %d, b = %d\n",a,b);
	getch();
}