C Program to Find Largest of Three Numbers Using Functions

Here we will write a C program to find the largest of three numbers using functions. In the flow-control topic, we already wrote a C program to find the largest among three numbers but without using any functions. Now, this time we will do the same task using function. Function divides the big programs into small ones. Our program is actually not a big program. For practice and understanding the function, purpose we are developing this program using function. First, we will solve the problem only using one function, later we will use more than one function.

C Program to Find the Largest of Three Numbers Using Functions

Program description:- Write a C program to find the largest among the three numbers. Take input from the user. Write a program in such a way that the input numbers may be integer, float, or both.

#include<stdio.h>

// function to find largest among three number
float large(float a, float b, float c)
{
   if(a>=b && a>=c) return a;
   else if(b>=a && b>=c) return b;
   else return c;
}

int main()
{
   float num1, num2, num3, largest;

   printf("Enter three numbers: ");
   scanf("%f %f %f", &num1, &num2, &num3);

   largest = large(num1, num2, num3);
   printf("Largest number = %.2f",largest);
   return 0;
}

Output for the different test-cases:-

Enter three numbers: 12.5 8 6.9
Largest number = 12.50

Enter three numbers: 3 5.9 6.0
Largest number = 6.00

C Program to Find the Largest of Three Numbers Using More Than One Function

Program description:- Write a C program to find the largest of three numbers. Define three functions input(), large() and display() to perform the operations. The function input() will take input from the user, the large() function will find the largest number, and the display() function print the result on the screen. Don’t do any operations inside the main function except calling the functions and storing their returning values.

#include<stdio.h>

// functions declaration
float input();
float large(float a, float b, float c);
void display(float max);

// main function
int main()
{
   float num1, num2, num3, largest;

   num1 = input();
   num2 = input();
   num3 = input();

   largest = large(num1, num2, num3);
   display(largest);

   return 0;
}

// function for taking input from user
float input()
{
   float n;
   printf("Enter number: ");
   scanf("%f", &n);
   return n;
}

// function to find sum of two numbers
float large(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 displaying the result
void display(float max)
{
   printf("Largest number = %.2f",max);
}

Output for the different test-cases:-

Enter number: 10
Enter number: 12
Enter number: 9
Largest number = 12.00

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Enter number: 9.5
Enter number: 4.51
Enter number: 2.99
Largest number = 9.50

Explanation

In this program, three functions input(), large() and display() is defined. Inside the main method, four variables are declared. The variables num1, num2, and num3 will store the numbers.

The input() function has no parameter because it doesn’t require any data from the caller function. It is defined as taking input from the end-user and return back to the caller function. The input() function returns only one value at a time. Hence, for three values, the input() function is called three times in the main function. (For returning three values at a time we need to use the array concept).

Now, the three values are stored in the variables of the main function. For finding largest number, the function large() is called with arguments num1, num2, and num3. The large() function has three parameters a, b, and c. The parameters will store the values of arguments. The value of num1 will be stored in the local variable ‘a’. Similarly, the value of num2 is copied to ‘b’ and the value of num3 is copied to variable ‘c’.

Inside the large() function, if the value of the variable ‘a’ is greater than equal to the value of the variables ‘b’ and ‘c’ then the value of ‘a’ is the largest among three, hence the value of ‘a’ is returned to the main function. If ‘a’ is not bigger then the else-if condition will be checked. When the else-if condition is true then the value of ‘b’ will be returned otherwise else block is executed and ‘c’ value is returned.

The display function takes the variable ‘largest’ as an argument and prints the result message to the screen.

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

3 thoughts on “C Program to Find Largest of Three Numbers Using Functions”

  1. Wonderful blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thanks

  2. Daftar Playsbo

    Nice post. I learn something totally new and challenging on websites I stumble upon every day. It’s always exciting to read content from other writers and use something from other sites.

Leave a Comment

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