Programs with Functions

 Program with default funtions

#include<stdio.h>
show()
{
    printf("Hy, Vishal");
}
void main()
{
    show();
}

Output:





Program with function with parameter for arithmetic operations

#include<stdio.h>
int add(int a,int b)
{
    printf("add %d",a+b);
}
int sub(int a,int b)
{
    printf("\n sub %d",a-b);
}
int mul(int a,int b)
{
    printf("\n mul %d",a*b);
}
int div(float a,float b)
{
    printf("\n div  %f",a/b);
}
int avg(float a,float b)
{
    float c=(a/b)*100;
    printf("\n add  %f",c);
}
void main()
{
    add(12,13);
    sub(45,6);
    mul(12,6);
    div(100,5);
    avg(574,700);
}

Output:







Program with function with return type

#include<stdio.h>
int add(int c,int b)
{
    return c+b;
}
void main()
{
    int a,b,c;
    a=10;
    b=30;
    c=add(a,b);
    printf("addition = %d",c);
}

Output:






Program to swap two values with fun with arguements (pass by reference)

#include<stdio.h>
#include<conio.h>
int fun(int *x,int *y)
{
    int c=0;
    c=*x;
    *x=*y;
    *y=c;

}
void main()
{  
    int a=9,b=1;
   
    fun(&a,&b);
    printf("\n a=%d \n b=%d",a,b);
}

Output:






























No comments:

Post a Comment