Functions – Theory and Programs [Learn to code]

What are Functions? Learn syntax for Functions, types of Functions and programs on Functions.

  • 060. Write a program to accept two numbers from user and print it’s addition,subtraction,multiplication,division using different functions in C language
     
    #include<stdio.h>
    #include<conio.h>
     
    int add(int x,int y)
    {
     return(x+y);
    }
     
    int sub(int x,int y)
    {
     return(x-y);
    }
     
    int mul(int x,int y)
    {
     return(x*y);
    }
     
    int div(int x,int y)
    {
     return(x/y);
    }
     
    main()
    {
    	int a,b;
    	clrscr();
    	printf("Enter numbers : n");
    	scanf("%d%d",&a,&b);
    	printf("nAddition       : %dn",add(a,b));
    	printf("nSubtraction    : %dn",sub(a,b));
    	printf("nMultiplication : %dn",mul(a,b));
    	printf("nDivision       : %dn",div(a,b));
    getch();
    }
  • 061. Write a program to accept a number from user and print it’s factorial, check if it prime or not , and print it’s fibbonacci series using different functions in C language
     
    #include<stdio.h>
    #include<conio.h>
     
    fact(int x)
    {
    	int i,fact=1;
    	for(i=1;i<=x;i++)
    		fact=fact*i;
    	printf("Factorial is : %d",fact);
    }
     
    IsPrime(int x)
    {
    	int i;
    	for(i=2;i<x-1;i++)
    	{
    		if(x%i==0)
    			{
    			printf("nNot a Prime Numbern");
    			return 0;
    			}
    	}
    	printf("nIt is a Prime numbern");
    }
     
    fibbo(int x)
    {
    	int pre=1,cur=1,i,temp;
    	printf("%d %d",pre,cur);
    	for(i=3;i<=x;i++)
    	{
    		temp=cur;
    		cur=pre+cur;
    		pre=temp;
    		printf(" %d",cur);
    	}
    }
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	fact(n);
    	IsPrime(n);
    	fibbo(n);
            getch();
    }
  • 062. Write a program to calculate square and cube of a given number in C language
     
    #include<stdio.h>
    #include<conio.h>
     
    int square(int x)
    {
    	return(x*x);
    }
     
    int qube(int x)
    {
    	return(x*x*x);
    }
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number.....");
    	scanf("%d",&n);
    	printf("nSquare : %dn",square(n));
    	printf("nQube   : %dn",qube(n));
    	qube(n);
    	getch();
    }

062. Write a program to calculate square and cube of a given number in C language

 
#include<stdio.h>
#include<conio.h>
 
int square(int x)
{
	return(x*x);
}
 
int qube(int x)
{
	return(x*x*x);
}
main()
{
	int n;
	clrscr();
	printf("Enter number.....");
	scanf("%d",&n);
	printf("\nSquare : %d\n",square(n));
	printf("\nQube   : %d\n",qube(n));
	qube(n);
	getch();
}

061. Write a program to accept a number from user and print it’s factorial, check if it prime or not , and print it’s fibbonacci series using different functions in C language

 
#include<stdio.h>
#include<conio.h>
 
fact(int x)
{
	int i,fact=1;
	for(i=1;i<=x;i++)
		fact=fact*i;
	printf("Factorial is : %d",fact);
}
 
IsPrime(int x)
{
	int i;
	for(i=2;i<x-1;i++)
	{
		if(x%i==0)
			{
			printf("\nNot a Prime Number\n");
			return 0;
			}
	}
	printf("\nIt is a Prime number\n");
}
 
fibbo(int x)
{
	int pre=1,cur=1,i,temp;
	printf("%d %d",pre,cur);
	for(i=3;i<=x;i++)
	{
		temp=cur;
		cur=pre+cur;
		pre=temp;
		printf(" %d",cur);
	}
}
main()
{
	int n;
	clrscr();
	printf("Enter number...");
	scanf("%d",&n);
	fact(n);
	IsPrime(n);
	fibbo(n);
        getch();
}

060. Write a program to accept two numbers from user and print it’s addition,subtraction,multiplication,division using different functions in C language

 
#include<stdio.h>
#include<conio.h>
 
int add(int x,int y)
{
 return(x+y);
}
 
int sub(int x,int y)
{
 return(x-y);
}
 
int mul(int x,int y)
{
 return(x*y);
}
 
int div(int x,int y)
{
 return(x/y);
}
 
main()
{
	int a,b;
	clrscr();
	printf("Enter numbers : \n");
	scanf("%d%d",&a,&b);
	printf("\nAddition       : %d\n",add(a,b));
	printf("\nSubtraction    : %d\n",sub(a,b));
	printf("\nMultiplication : %d\n",mul(a,b));
	printf("\nDivision       : %d\n",div(a,b));
getch();
}