A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

C Program: Biggest of Three numbers using function

Using function we can easily find the biggest of three numbers. By dividing the program into functions we can solve the given problem easily. Functions are the easiest solutions to solve any given problem.

Definition:

A Function is a group of statements that together perform a common task. Every C program must have at least one function, which is main().

Description:
  • Here we are using function biggest() to find the largest number.
  • Function biggest() uses the nested if else statement to find the greatest number.
  • It receives any three integer or float numbers and returns the biggest number.

Algorithm to calculate biggest of 3 numbers using function:

STEP 1: START
STEP 2: READ any three numbers
              READ a, b, c
STEP 3: CALL function big = biggest()
              COMPARE a with b and c
              Compare a and b
              if a >= b
              if a is greater than b compare a with c
              if a >= c
                            RETURN a
              Otherwise
                            RETURN c
              OTHERWISE COMPARE b with c
               if b >= c
                             RETURN b
              Otherwise
                             RETURN c
STEP 4: PRINT biggest number
              PRINT big
STEP 5: END

C Program to find biggest of three numbers using biggest() function:

#include <stdio.h>
#include <conio.h>
float biggest(float a,float b,float c);
int main()
{
    float a,b,c,big;
    printf("Enter the three numbers: ");
    scanf("%f %f %f",&a,&b,&c);
    big=biggest(a,b,c);
    printf("Biggest number is: %f",big);
    getch();
    return 0;
}
float biggest(float a,float b,float c)
{
  if(a>=b)
  {
      if(a>=c)
        return a;
      else
          return c;
  }
  else
  {
      if(b>=c)
          return b;
      else
          return c;
  }
}

Output 1:

Enter the three numbers: 11 4 55
Biggest number is: 55.000000

Output 2:

Enter the three numbers: 1.1 2.5 -13
Biggest number is: 2.500000

Additional information:

if else statement:

It is a decision making statement.
It is a two way branching statement.
If the condition evaluates to true, statements which are inside the if block are executed.
If the condition evaluates to false, statements which are inside the else block are executed.

A function definition has following parts:

Return Type − A function may or may not return a value. The return_type represents the type of value it is going to return.
Function Name − It is any valid identifier. The name and the parameter list together constitute the function header.
Parameter list− A parameter is a placeholder. The parameter list refers to the type, order and number of parameters.
Function Body − The body contains group of statements that perform common task.