• # C Program to Find the Largest Number Among Three Numbers

Welcome to this blog post where we will discuss and learn how to write a C program to find the largest number among three numbers using a function. This program is a fundamental concept in programming and will help you understand the basics of functions and conditional statements in the C programming language. Whether you are new to programming or have some prior experience, this post will provide step-by-step guidance to solve this problem.

## Introduction

Have you ever wondered how computers can determine the largest number among a set of numbers? Well, it’s all about using the power of programming. In this program, we will introduce the concept of functions in C and demonstrate their use in finding the largest number among three given numbers.

The purpose of this post is to explain the program in a clear and concise manner, ensuring that even beginners can easily follow along. By the end of this blog post, you will have a solid grasp of using functions and conditional statements to solve this problem.

So, without further ado, let’s dive into the details!

## What is a Function?

Functions serve as blocks of reusable code that perform specific tasks. They allow you to break down complex programs into smaller, manageable pieces. In C, a function consists of a function declaration and a function definition.

### Function Declaration

A function declaration specifies the function’s name, return type, and any parameters it requires. In our case, we will declare a function called `findLargestNumber` that takes three integer parameters.

### Function Definition

The function definition includes the actual code that executes when the function is called. In our code, the `findLargestNumber` function will compare the three numbers and return the largest among them using conditional statements.

## Writing the C Program

To write the program, we will follow these steps:

1. Include the necessary header files.

2. Declare the `findLargestNumber` function.

3. Implement the function to find the largest number.

4. Write the `main` function to input the numbers and call the `findLargestNumber` function.

5. Display the result.

Let’s break down these steps and discuss them in detail.

#### Step 1: Include the necessary header files

The first step is to include the necessary header files in our program. In this case, we need the `stdio.h` header file to work with input and output functions.

``#include <stdio.h>``

#### Step 2: Declare the `findLargestNumber` function

Next, we need to declare the `findLargestNumber` function. The function declaration specifies its return type and the parameters it takes. In our case, this function will return an integer and take three integer parameters representing the numbers to compare.

``int findLargestNumber(int num1, int num2, int num3);``

#### Step 3: Implement the `findLargestNumber` function

After declaring the function, we can implement it to find the largest number among the three given numbers. This function will use conditional statements (`if` and `else`) to compare the numbers and determine the largest one.

Here’s how the implementation looks:

``````int findLargestNumber(int num1, int num2, int num3) {
int largest;

if (num1 >= num2 && num1 >= num3) {
largest = num1;
} else if (num2 >= num1 && num2 >= num3) {
largest = num2;
} else {
largest = num3;
}

return largest;
}``````

Let’s understand the code logic a bit. We start by initializing a variable `largest` to hold the largest number. Then, using conditional statements, we compare `num1` with `num2` and `num3`. If `num1` is greater than or equal to both `num2` and `num3`, it is considered the largest. Similarly, we compare `num2` with `num1` and `num3`, and `num3` with `num1` and `num2` to find the largest number.

#### Step 4: Write the `main` function

The `main` function serves as the entry point of our program. Here, we will take input from the user for the three numbers and display the largest number by calling the `findLargestNumber` function.

``````int main() {
int num1, num2, num3;

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

int largest = findLargestNumber(num1, num2, num3);

printf("The largest number is %d\n", largest);

return 0;
}``````

In the `main` function, we declare three integer variables `num1`, `num2`, and `num3` to store the user input. We then prompt the user to enter the three numbers using the `printf` function and read them using the `scanf` function.

Next, we call the `findLargestNumber` function and pass the three input numbers as arguments. The function returns the largest number, which we store in the `largest` variable. Finally, we use the `printf` function again to display the largest number.

#### Step 5: Display the result

The last step is to display the result, which we have already done in the previous step. The `printf` function is used to print the largest number.

## Running the Program

To run the program, you need to compile and execute it. Save the code in a file with a `.c` extension, such as `largest_number.c`. Open a terminal or command prompt and navigate to the directory where the file is saved.

Compile the code using a C compiler, such as GCC, by running the following command:

``gcc -o largest_number largest_number.c``

This will generate an executable file named `largest_number`. Execute the program by running the following command:

``./largest_number``

Now, you can enter the three numbers when prompted, and the program will display the largest number.

## Conclusion

In this blog post, Finally, we have learned how to write a C program to find the largest number among three numbers using a function. We discussed the concepts of functions, function declaration, function definition, and conditional statements.

By breaking down the program into smaller steps, we were able to solve the problem of finding the largest number in a structured manner. This approach not only helps in understanding the program better but also allows us to reuse the code whenever needed.

Remember, practice is key to mastering any programming concept. I encourage you to try different variations of the program, explore edge cases, and experiment with different inputs. This will solidify your understanding and improve your problem-solving skills.

If you want to delve deeper into C programming, there are countless resources available online, including tutorials, books, and practice exercises. Keep exploring, keep coding, and enjoy your programming journey!

Happy coding!