Goal: To develop a program that determines and displays the largest number among three input values (a, b, and c).
C Program:
#include <stdio.h> int main() { // Declare variables to hold three numbers and the largest number float a, b, c; // Request user input for three numbers printf("Enter three numbers: "); scanf("%f %f %f", &a, &b, &c); // Check for equality of all three numbers if (a == b && b == c) { printf("All numbers are equal.\n"); } // Determine the largest number else { // Calculate maximum of a and b float max_ab = a >= b ? a : b; // Compare max_ab and c to find the overall largest number float largest = max_ab >= c ? max_ab : c; // Display the largest number printf("The largest number is %f.\n", largest); } return 0; }
Explanation:
- We include the necessary header file,
<stdio.h>
, for input and output operations. - The program starts with the
main
function. - Three
float
variables (a
,b
, andc
) are declared to store the input numbers and the largest number. - The user is prompted to enter three numbers, which are read using
scanf
and stored in the respective variables. - We first check if all three numbers are equal using the condition
if (a == b && b == c)
. - If all numbers are equal, we display a message indicating that.
- Otherwise, we proceed to find the largest number.
- We calculate the maximum of
a
andb
using the ternary operator. - If
a
is greater than or equal tob
,max_ab
is assigned the value ofa
; otherwise, it is assigned the value ofb
. - We then compare
max_ab
andc
to determine the overall largest number using the same ternary operator approach. - Finally, we display the largest number using
printf
.
This program efficiently finds and displays the largest number among three input values, handling the case of equal numbers as well.