0% found this document useful (0 votes)
7 views10 pages

User-Defined Functions in C Programming

This document contains programming realted to functions
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

User-Defined Functions in C Programming

This document contains programming realted to functions
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Example1: Creating a user defined function addition()

#include <stdio.h>
int addition(int num1, int num2)
{
int sum;
/* Arguments are used here*/
sum = num1+num2;

/* Function return type is integer so we are returning


* an integer value, the sum of the passed numbers.
*/
return sum;
}

int main()
{
int var1, var2;
printf("Enter number 1: ");
scanf("%d",&var1);
printf("Enter number 2: ");
scanf("%d",&var2);

/* Calling the function here, the function return type


* is integer so we need an integer variable to hold the
* returned value of this function.
*/
int res = addition(var1, var2);
printf ("Output: %d", res);

return 0;
}

Example2: Creating a void user defined function that doesn’t return anything
#include <stdio.h>
/* function return type is void and it doesn't have parameters*/
void introduction()
{
printf("Hi\n");
printf("My name is Chaitanya\n");
printf("How are you?");
/* There is no return statement inside this function, since its
* return type is void
*/
}

int main()
{
/*calling function*/
introduction();
return 0;
}

E3: Square root

#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
// main function, program starts from here

int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
// function call
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}

float square ( float x ) // function definition


{
float p ;
p=x*x;
return ( p ) ;
}

E4:
#include <stdio.h>
int add(int a, int b); //function declaration
int main()
{
int a=10,b=20;
int c=add(10,20); //function call
printf("Addition:%d\n",c);
getch();
}
int add(int a,int b) //function body
{
int c;
c=a+b;
return c;
}
E5:Find area
#include<stdio.h>

// function declaration
float findArea(float l, float b);

int main()
{
float length, width, result;

printf("Enter the length and width of the rectangle: ");


scanf("%f %f", &length, &width);

result = findArea(length, width); //function calling


printf("Area of rectangle = %.2f\n",result);

return 0;
}

// function to find ara of rectangle


// function definition
float findArea(float l, float b)
{
float area;
area = l * b;
return area; //return statement
}

E6:Prime Numbers Between Two Integers


#include <stdio.h>
int checkPrimeNumber(int n);
int main() {

int n1, n2, i, flag;

printf("Enter two positive integers: ");


scanf("%d %d", &n1, &n2);

// swap n1 and n2 if n1 > n2


if (n1 > n2) {
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
}

printf("Prime numbers between %d and %d are: ", n1, n2);


for (i = n1 + 1; i < n2; ++i) {

// flag will be equal to 1 if i is prime


flag = checkPrimeNumber(i);

if (flag == 1) {
printf("%d ", i);
}
}

return 0;
}

// user-defined function to check prime number


int checkPrimeNumber(int n) {
int j, flag = 1;

for (j = 2; j <= n / 2; ++j) {

if (n % j == 0) {
flag = 0;
break;
}
}

return flag;
}

E7:
#include<stdio.h>

// function declaration
float circleArea(float r);

int main()
{
float radius, area;

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

area = circleArea(radius); //function calling


printf("Area of circle = %.2f\n",area);

return 0;
}

// function definition
float circleArea(float r)
{
float area = 3.14 * r * r;
return area; // return statement
}

E8:
#include<stdio.h>

// function declaration
float cube(float a);

int main()
{
float num, result;

printf("Enter a number: ");


scanf("%f", &num);

result = cube(num); //function calling


printf("Cube of %.2f = %.2f\n", num, result);

return 0;
}

// function definition
float cube(float a)
{
float result = a*a*a;
return result; //return statement
}
E9:
#include<stdio.h>

// function declaration
float absolute(int n);

int main()
{
int num, result;

printf("Enter a number: ");


scanf("%d", &num);

result = absolute(num); //function calling


printf("Absolute value of %d = %d\n", num, result);

return 0;
}

// function definition
float absolute(int n)
{
if(n<0) return -n;
else return n;
}

E10:
#include<stdio.h>

// function declarations
float minimum(float a, float b, float c);
float maximum(float a, float b, float c);

int main()
{
float n1, n2, n3;

printf("Enter three numbers: ");


scanf("%f %f %f", &n1, &n2, &n3);

printf("Minimum number = %.2f\n",minimum(n1,n2,n3));


printf("Maximum number = %.2f\n",maximum(n1,n2,n3));

return 0;
}

// function for finding minimum of three numbers


float minimum(float a, float b, float c)
{
if(a<=b && a<=c) return a;
else if(b<=a && b<=c) return b;
else return c;
}

// function for finding maximum of three numbers


float maximum(float a, float b, float c)
{
if(a>=b && a>=c) return a;
else if(b>=a && b>=c) return b;
else return c;
}

E11:
#include<stdio.h>

// function declarations
int getNumber();
int square(int n);
void display(int n, int sqr);

int main()
{
int num = getNumber();
int result = square(num);
display(num, result);
return 0;
}

// function for get input from the user


int getNumber()
{
int n;
printf("Enter an Integer number: ");
scanf("%d",&n);
return n;
}

// function for finding square value


int square(int n)
{
return n*n;
}

// function for displaying result


void display(int n, int sqr)
{
printf("Square of %d = %d\n", n, sqr);
}

E12:
#include<stdio.h>

// function declarations
int addTwoDigits(int n);
int lastDigit(int n);
int secondLastDigit(int n);

int main()
{
int number;

printf("Enter an integer number: ");


scanf("%d",&number);

int sum = addTwoDigits(number);


printf("Sum of last two digits is = %d",sum);

return 0;
}

// function for adding last two digit


int addTwoDigits(int n)
{
int result = lastDigit(n) + secondLastDigit(n);
return result;
}

// function for finding last digit


int lastDigit(int n)
{
return (n%10);
}

// function for finding second last digit


int secondLastDigit(int n)
{
return ((n/10)%10);
}

E13:
#include<stdio.h>
int check(int a);
int main()
{
int n, j, flag;

printf("Enter a number: ");


scanf("%d",&n);

// divide number into two parts j and (n-j)


// such as n = j + (n-j)
for(j=2;j<=n/2;j++)
{
// condition for j to be a prime number
if(check(j)==0)
{
// condition for n-j to be a prime number
if(check(n-j)==0)
{
// n= j + (n-j)
printf(" %d + %d = %d\n", j, n-j, n);
flag=0;
}
}
}

if(flag==1)
printf("Can't be expressed as sum of two prime Numbers.\n");

return 0;
}

int check(int a)
{
int i, flag=0;

for(i=2;i<=a/2;i++)
{
if(a%i==0)
flag++;
}

if(flag==0)
return 0;
else
return 1;
}

E14:

#include
void swapnum ( int *var1, int *var2 )
{
int tempnum ;
tempnum = *var1 ;
*var1 = *var2 ;
*var2 = tempnum ;
}
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);

/*calling swap function*/


swapnum( &num1, &num2 );

printf("\nAfter swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);
return 0;
}

You might also like