Find the Largest Number: A Comprehensive C Programming Tutorial

LabEx
2 min readJun 25, 2024

--

Cover

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>

Declare variables

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

float a, b, c;

Get user input

We can now prompt the user for their input.

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

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);
}

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;

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 ! 😄

--

--

LabEx
LabEx

Written by LabEx

LabEx is an AI-assisted, hands-on learning platform for tech enthusiasts, covering Programming, Data Science, Linux and other areas.

No responses yet