Program to find min and max using functions in c language

min-and-max-using-functions-in-c-programming-language

Min and Max using functions in C Program Description:

Write a program to find the min and max using functions in C language. We are going to write two programs, One for calculating the minimum and another for calculating the maximum.

Pre-Requisites:

Find Min using a function in C:

Input:

Enter two numbers: 92 31

Output:

The minimum of 92 and 31 is : 31

Find the Minimum using a function in C language Algorithm:

  1. Take two numbers from the user and store them in variables num1 and num2 respectively.
  2. We defined a function called min, The min function accepts two integer numbers as input and calculates the minimum of the given numbers. Here is the prototype of the min function
    • int min(int num1, int num2);
    • function_name: min
    • arguments_list : (int, int)
    • return_type: int
    • The min function uses the conditional variable to calculate the minimum of num1 and num2(i.e (num1 > num2) ? num2 : num1 ) and returns a minimum value.
  3. Call the min function passing input numbers num1 and num2. and store the return value in variable minimum
  4. Print the results on the console.

Program to Find the Minimum using a function in C language:

Minimum Program output:

Save the above program with .c extension. Compile and run the program.

minimum-of-two-numbers-using-function-in-c-language

Let’s try a few more examples.

Find a Maximum of Two numbers using a function in C:

Example Input:

Enter two numbers: 4 2

Output:

The maximum of 4 and 2 is : 4

Find a Max of two numbers using a function Program algorithm:

  1. Accept two integers from the user and store them in variables num1 and num2 respectively.
  2. We have created a function called max, The max function accepts two integer numbers as input and calculates the maximum of the given numbers. Here is the prototype of the max function
    • int max(int num1, int num2);
    • function_name: min
    • arguments_list : (int, int)
    • return_type: int
    • The max function uses the conditional variable to calculate the maximum of num1 and num2 (i.e (num1 > num2) ? num2 : num1 ) and returns a maximum value.
  3. Call the max function from the main function and pass two variables num1 and num2. Store the return value in maximum variable.
  4. We got the max value in maximum variable, Print the result on the console.

Program to find max of Two numbers using function in C:

Program Output:

Compile and Run the program.

$ gcc max-function.c

The above gcc command, Generates the a.out executable file. Run the executable using ./a.out command.

max-of-two-numbers-in-c-using-function

Let’s try a few more examples.

As expected, We are getting the results correctly.

Related Programs:

  1. All C Programs Index [List]
  2. C Program to Calculate Sum of Digits of a Number
  3. C Program to Calculate Product of Digits of a Number
  4. C Program to Count the number of Digits of a Number
  5. C Program to Multiply without using Multiplication operator (*)
  6. C Program to generate Fibonacci Series up to a given number
  7. C Program to Print First n Fibonacci numbers
  8. C Program to get Nth Fibonacci number

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply