Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

C program to find the third largest element in an array using conditional checks

Find Third Largest Element in Array - C Program

πŸ”’ Find the Third Largest Element in an Array (C Program)

#include <stdio.h>

int main()
{
    int num, a[100];
    printf("Enter the size of the array:\n");
    scanf("%d", &num);

    if (num < 3)
    {
        printf("Invalid! Need at least 3 elements to find the third largest element.\n");
        return 1;
    }

    printf("Enter %d elements:\n", num);
    for (int i = 0; i < num; i++)
    {
        scanf("%d", &a[i]);
    }

    int first, second, third;

    // Sort first 3 elements
    if (a[0] >= a[1] && a[0] >= a[2])
    {
        first = a[0];
        if (a[1] >= a[2])
        {
            second = a[1];
            third = a[2];
        }
        else
        {
            second = a[2];
            third = a[1];
        }
    }
    else if (a[1] >= a[0] && a[1] >= a[2])
    {
        first = a[1];
        if (a[0] >= a[2])
        {
            second = a[0];
            third = a[2];
        }
        else
        {
            second = a[2];
            third = a[0];
        }
    }
    else
    {
        first = a[2];
        if (a[0] >= a[1])
        {
            second = a[0];
            third = a[1];
        }
        else
        {
            second = a[1];
            third = a[0];
        }
    }

    for (int i = 3; i < num; i++)
    {
        if (a[i] > first)
        {
            third = second;
            second = first;
            first = a[i];
        }
        else if (a[i] > second && a[i] != first)
        {
            third = second;
            second = a[i];
        }
        else if (a[i] > third && a[i] != second && a[i] != first)
        {
            third = a[i];
        }
    }

    if (first == second || second == third)
    {
        printf("There is no distinct third largest element.\n");
    }
    else
    {
        printf("The third largest element is: %d\n", third);
    }

    return 0;
}
  

πŸ“˜ Explanation:

✅ First, the code reads the number of elements and validates it should be at least 3.
✅ It initializes the first, second, and third largest elements by sorting the first 3 manually.
✅ Then it iterates from the 4th element to update first, second, and third as needed.
✅ Finally, it ensures all 3 values are distinct before printing the third largest.

πŸ§ͺ Sample Output:

Enter the size of the array:
6
Enter 6 elements:
1 4 5 2 9 7
The third largest element is: 5
    

🏷️ Keywords:

C program for third largest number, top 3 numbers in array, array sorting in C, C max elements, competitive programming

Comments

πŸŒ™