Greatest of three numbers

WE will ask user to enter three numbers. We will store then in three variables and then compare with each other by using if else conditional statement.
Here is the sample code.
#include <stdio.h>

int main(void) {
    int a, b, c;

    printf("Enter the numbers: ");
    scanf("%d %d %d", &a, &b, &c);  // Input numbers

    if (a > b) {
        if (a > c) {
            printf("Highest number is a = %d", a);
        } else {
            printf("Highest number is c = %d", c);
        }
    } else {
        if (b > c) {
            printf("Highest number is b = %d", b);
        } else {
            printf("Highest number is c = %d", c);
        }
    }

    return 0;
}

Solution using AND ( && ) logical conditions

#include <stdio.h>

int main(void) {
    int a, b, c;

    printf("Enter the numbers: ");
    scanf("%d %d %d", &a, &b, &c);  // Input numbers

    if (a > b && a > c) {
        printf("Greatest number is a: %d", a);
    } else if (b > c && b > a) {
        printf("Greatest number is b: %d", b);
    } else if (c > a && c > b) {
        printf("Greatest number is c: %d", c);
    }

    return 0;
}

Example 2: Finding the Highest Number in an Array of Floating Point Numbers

Here’s an example to find the highest number in an array of floating-point numbers:

#include <stdio.h>

int main() {
    float arr[] = {1.2, 3.5, 5.7, 2.8, 4.6};
    int n = sizeof(arr) / sizeof(arr[0]);
    float max = arr[0];

    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    printf("The highest number is: %.2f\n", max);

    return 0;
}

Finding the Highest Number in an Array

This C program finds the highest number in an array of floating-point numbers using a for loop.

Code Explanation:

  • Define an array: A floating-point array is initialized with values.
  • Calculate the number of elements: The sizeof operator determines the total count.
  • Initialize max value: The first element of the array is assigned as the highest value initially.
  • Iterate through the array: A for loop checks each element to find the highest value.
  • Compare and update: If an element is greater than the current max, it updates the max value.
  • Display result: The highest value is printed with two decimal precision.

Example Output:

The highest number is: 5.70

Example 3: Finding the Highest Number Using a Function

Encapsulate the logic to find the highest number in a function for reusability:

#include <stdio.h>

float findMax(float arr[], int n) {
    float max = arr[0];

    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

int main() {
    float arr[] = {3.1, 4.5, 2.9, 6.7, 5.5};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("The highest number is: %.2f\n", findMax(arr, n));

    return 0;
}

This C program finds the highest number in an array of floating-point numbers using a separate function.

Code Explanation:

  • Function findMax(): This function takes an array and its size as parameters and returns the highest value.
  • Initialize max value: The first element of the array is assigned as the highest value initially.
  • Iterate through the array: A for loop checks each element to find the highest value.
  • Compare and update: If an element is greater than the current max, it updates the max value.
  • Main function: Declares an array and calls findMax() to get the highest number.
  • Display result: The highest value is printed with two decimal precision.

Example Output:

The highest number is: 6.70

Example 4: Highest Number in an Array Entered by the User

Allow the user to input the array and find the maximum value:

#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n];

    for (int i = 0; i < n; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &arr[i]);
    }

    int max = arr[0];

    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    printf("The highest number is: %d\n", max);

    return 0;
}

This C program allows the user to enter a set of numbers and determines the highest number among them.

Code Explanation:

  • Input the number of elements: The program first asks the user how many numbers they want to enter.
  • Declare an array: An integer array is created to store the input values.
  • Use a for loop: The program takes user input and stores it in the array.
  • Initialize the max value: The first element is assumed to be the highest initially.
  • Find the highest number: Another for loop iterates through the array and compares each value with the current max.
  • Display the result: The highest number is printed on the screen.

Example Output:

Enter the number of elements: 5
Enter number 1: 12
Enter number 2: 45
Enter number 3: 23
Enter number 4: 67
Enter number 5: 34
The highest number is: 67



plus2net.com






We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer