Find Largest Number

CCBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123277{{"`Find Largest Number`"}} c/comments -.-> lab-123277{{"`Find Largest Number`"}} c/variables -.-> lab-123277{{"`Find Largest Number`"}} c/data_types -.-> lab-123277{{"`Find Largest Number`"}} c/operators -.-> lab-123277{{"`Find Largest Number`"}} c/if_else -.-> lab-123277{{"`Find Largest Number`"}} c/user_input -.-> lab-123277{{"`Find Largest Number`"}} c/memory_address -.-> lab-123277{{"`Find Largest Number`"}} c/function_parameters -.-> lab-123277{{"`Find Largest Number`"}} c/function_declaration -.-> lab-123277{{"`Find Largest Number`"}} end

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.

Other C Tutorials you may like