C Program Using Functions Example

Here we will develop the C program using functions. We will write user-defined functions to solve the problems.

After learning the introduction to function in C and User-defined function in C, we are able to build some user-defined functions to perform some mathematical operations like finding the area of a rectangle, circle, cube of the number, absolute value of a number and many others.

Some examples you will found on this page and some other important examples of the function you will found on the given links on this page. First, develop the basic user-defined function given on this page and then work on important programs on the function linked from this page. In this way, you will get ideas about how user-defined functions are written in different situations. Remember one thing:- Practice is everything and it makes perfect.

C Program to Find the Area of a Rectangle Using Functions

Program description:- Write a C program using a user-defined function to find the area of a rectangle. Take input from the user.

Formula:-
The area of a rectangle is length * width.

#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
}

Output:-

Enter the length and width of the rectangle: 12.5 5
Area of rectangle = 62.50

In this C program using function area of a rectangle is calculated. Firstly, the function findArea is defined, it will return a floating-point number and it has two parameters. After taking input from the user in the main function, the function findArea is called with argument length and width. The function findArea calculates the area of the rectangle, and the value of the area is returned to the caller function main. Finally, the result is displayed on the screen.

C Program to Find the Area of a Circle Using Functions

Program description:- Write a C program with a user-defined function to find the area of a rectangle. Take input from the user.

Formula:-
Area of circle = 3.14*r*r;
3.14 is the value of PI.

#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
}

Output:-

Enter the radius of the circle: 9
Area of circle = 254.34

In this C program using the function area of a circle is calculated. Execution of the program always starts from the main function. After taking input from the user, the function circleArea is called. This function takes an argument, store the value of an argument into a parameter variable, and using the area of a circle formula, it calculates the area. Later result is passed back to the main function. Finally, the result is displayed on the screen.

C Program to Find the Cube of a Number Using Functions

Program description:- Write a program to find the cube of a given number by defining a user-defined function. Take input from the user.

#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
}

Output:-

Enter a number: 6
Cube of 6.00 = 216.00

The cube of a number is calculated using mathematical expression n^3, where n is the number. In this program, the function cube calculates the cube of the number. The number is passed as an argument from the main function. The function cube calculates the cube value and returns it back to the main function.

For practice purposes, similarly like finding the cube of a number, write another program to find the square of a number using user-defined functions.

Find the Absolute Value of a Number by Defining a User-defined Function

Program description:- Write a C program to find the absolute value of a given number without using abs() function.

#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;
}

Output for the different test cases:-

Enter a number: 5
Absolute value of 5 = 5

Enter a number: -9
Absolute value of -9 = 9

In this C program, we find the absolute value of a number by defining a user-defined function. In the mathematical library header file, one function abs(x) is available for finding the absolute value of the number. But in this program, we need to write our own function to find the absolute value of the number. So, the function absolute is defined in this program which takes an argument from the main function and returns the absolute value of the number.

Find Minimum & Maximum in Three Numbers by Function

Program description:- In the C program write a user-defined function to find minimum and maximum in given three numbers. Take three numbers from the user.

#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;
}

Output:-

Enter three numbers: 10.2 3.5 5.9
Minimum number = 3.50
Maximum number = 10.20

For finding the minimum of three number one function minimum is defined and for finding the maximum of three number another function maximum is defined. Both functions take three values as arguments and after doing its own task, it returns the value back to the main function.

Find Square of a Number Using more than One Functions

Program description:- Write a C program to find the square of a given number using user-defined functions. Define three functions, take input from the user using one function, calculate a square in another function, and display the result in another function.

#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);
}

Output:-

Enter an Integer number: 5
Square of 5 = 25

In large programs, the main function is written only with function calls. This is an example of decomposition, the process of breaking a complex problem into simple parts. One example is not really complex, but for a demonstration of the concept, this program is given. Our program has grown into four functions, including the main function.

In this program, three function getNumber, square, and display is defined. The function getNumber takes the input from the user and returns back it to the main function. In the main function returning value of the function, getNumber is stored in the variable num. Later main function call square function with argument num. The square function store it in parameter variable and return the square value. The main function store this value in the variable result and called another function display. The function display takes the value and displays it to the screen.

The function getNumber and square, both are non-void functions with one parameter. The display function is a void and parameterized function.

Find the Sum of Last Two Digits of an Integer Number

Program description:- Write a program that extracts and adds the two least significant digits of any number. Define three functions addTwoDigits(), lastDigit() and secondLastDigit(). The main function should call addTwoDigits(). The lastDigit() and secondLastDigit() functions should be called from function addTwoDigits().

#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);
}

Output for the different test-cases:-

Enter an integer number: 256
Sum of last two digits is = 11

Enter an integer number: 52368
Sum of last two digits is = 14

Check Number can be Expressed as the Sum of Two Prime Numbers

Program description:- Write a C program to check whether a number can be expressed as the sum of two prime numbers or not.

Number having only two factors ( 1 and itself ) called Prime number. Example:- 5,7,11,13 e.t.c.

Here we will check That whether a number can be expressed as the sum of two prime numbers or not. Ex:- 12=5+7, 5 and 7 are the prime numbers so, 10 can be expressed as the sum of two prime numbers.

#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;
}

Output for the different test-cases:-

Enter a number: 60
7 + 53 = 60
13 + 47 = 60
17 + 43 = 60
19 + 41 = 60
23 + 37 = 60
29 + 31 = 60

Enter a number: 20
3 + 17 = 20
7 + 13 = 20

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *