Notification texts go here Contact Us Buy Now!

A program to find the largest number among three numbers

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, and c) 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 and b using the ternary operator.
  • If a is greater than or equal to b, max_ab is assigned the value of a; otherwise, it is assigned the value of b.
  • We then compare max_ab and c 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.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.