Programs of Function in C

 

List of programs

  1. Write a program to print "HELLO C" using function.

  2. Write a program to calculate sum using function.

  3. Write a program to calculate square using function.

  4. Write a program to swap numbers using function.

  5. Write a program to calculate the ASCII value of an integer.

  6. Write a program to calculate the product of float and int.

  7. Write a program to calculate the value of a power b by using function without using pow() function.

  8. Write a program to calculate the factorial of any number using function.

  9. Write a program to convert upper character to lower and lower to upper using function without any predefined function.

1. Write a program to print "HELLO C" using function.

Program:

#include <stdio.h>

void main()
{
    // Function Prototype Declaration    
    void message(void); 

    //function call
    message(); 
   
    getch();
}

void message()
{
    printf("Hello C");
}

2. Write a program to calculate sum using function.

Program:

#include <stdio.h>

void main()
{
    //function prototype declaration
    int sum(int,int);

    int a,b,s;

    printf("Enter first number:");
    scanf("%d",&a);

    printf("Enter second number:");
    scanf("%d",&b);

    // function call
    s=sum(a,b);
    printf("Sum of two number is:%d",s);

    getch();
}

int sum(int x,int y)
{
    return(x+y);
}

3. Write a program to calculate square using function.

Program :

#include <stdio.h>

void main()
{
    //function prototype declaration
    int square(int);
    int a,b,sq;
    
    printf("Enter first number:");
    scanf("%d",&a);
    // function call
    sq=square(a);
    
    printf("Square of given number is %d",sq);
   
    getch();
}

int square(int x)
{
    int i;
    i=x*x;
    return(i);
}

4. Write a program to swap numbers using function.

Program:

#include <stdio.h>

void main()
{
    //function prototype declaration
    void swap(int,int);
    int a,b;
    
    //read numbers from user
    printf("Enter first number:");
    scanf("%d",&a);
    
    printf("Enter second number:");
    scanf("%d",&b);
    
    printf("Numbers before swap a=%d & b=%d",a,b);
    // function call
    sq=swap(a,b);
   
    getch();
}

void swap(int x,int y)
{
    int temp;
    temp=x;
    x=y;
    y=temp;
    printf("Numbers after swap a=%d & b=%d",x,y);
}

5. Write a program to calculate the ASCII value of an c.

Program:

#include <stdio.h>

int main()
{
    char ascii(int);
    int a;
    char d;
    printf("\n Enter one no:");
    scanf("%d",&a);
    d=ascii(a);
    printf("The ascii value of inputed no is %c",d);
    getch();
    
}
char ascii(int y)
{
    char r;
    r=y;
    return r;
}


6. Write a program to calculate the product of float and int.

Program:

#include <stdio.h>

int main()
{
    float product(float,int);
    float a,d;
    int b;
    printf("Enter two no:");
    scanf("%f%d",&a,&b);
    d=product(a,b);
    printf("\n%f",d);
    getch();
    
}

float product(float x,int y)
{
    float r;
    r=x*y;
    return r;
}


7. Write a program to calculate the value of a power b by using function without using pow() function.

Program:

#include <stdio.h>
#include <conio.h>

void main()
{
    int power(int,int);
    int a,b,d;
    printf("Enter two numbers:");
    scanf("%d%d",&a,&b);
    d=power(a,b);
    printf("\n%d to the power of %d is %d",a,b,d);
    getch();
    
}

int power(int x,int y)
{
    int r=1,i;
    for(i=1;i<=y;i++)
    {
        r=r*x;
    }
    return r;
}


8. Write a program to calculate the factorial of any number using function.

Program: 

#include <stdio.h>
#include <conio.h>
void main()

{
int fact(int);
int n;


printf (" Enter any number :");
scanf ("%d", &n);

printf (" Factorial of %d = %d\n",n,fact(n));

getch();
}

int fact(int n)
{
    int fact=1;
    for(int i=1;i<=n;i++)
    {
        fact=fact*i;
    }
    return fact;
}


9. Write a program to convert upper character to lower and lower to upper using function without any predefined function.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
    int convert(char);
    int a;
    char ch;
    printf("Enter Character:");
    scanf("%c",&ch);
    a=convert(ch);
    printf("%c",a);
    getch();
}

int convert(char x)
{
    int i,j;
    i=x;
    if(i>=65 && i<=96)
    {
        j=i+32;
    }
    if(i>=97 && i<=122)
    {
        j=i-32;
    }
    return j;
}

No comments:

Post a Comment