DEV Community

Cover image for Find the Largest Number: A Comprehensive C Programming Tutorial
Labby for LabEx

Posted on • Originally published at labex.io

Find the Largest Number: A Comprehensive C Programming Tutorial

Introduction

In this lab, we will write a C program to find the largest number among three user input numbers. We will prompt the user to enter three numbers, and then our program will determine the largest number and print it to the console.

Create a C program

First, we need to create a C program in the main.c file that is located in the ~/project/ directory.

Include necessary libraries

The first thing to do is to include the necessary header files.

#include <stdio.h>
Enter fullscreen mode Exit fullscreen mode

Declare variables

Next, we declare three variables of type float to hold the user input values.

float a, b, c;
Enter fullscreen mode Exit fullscreen mode

Get user input

We can now prompt the user for their input.

printf("Enter 3 numbers: ");
scanf("%f %f %f", &a, &b, &c);
Enter fullscreen mode Exit fullscreen mode

Find the highest number

Now, we will use an if...else statement to determine the largest number:

if(a >= b && a >= c)
{
   printf("\n\nLargest number = %.3f ", a);  // prints the largest number to the console
}
else if(b >= a && b >= c)
{
   printf("\n\nLargest number is = %.3f", b);
}
else
{
   printf("\n\nLargest number is = %.3f", c);
}
Enter fullscreen mode Exit fullscreen mode

Display the output

Lastly, we need to print the output to the console.

printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
Enter fullscreen mode Exit fullscreen mode

Make sure to compile and run the program to test it.

Summary

In this lab, we learned how to write a C program to find the largest of three numbers entered by the user. We used the if-else conditional statement to determine the largest number and printed the result to the console. Remember to always test your program thoroughly to ensure that it is working as expected.


Want to learn more?

Join our Discord or tweet us @WeAreLabEx ! 😄

Top comments (0)