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() { ...

Find Second Largest Element and Second Largest Element in Array in C

C Program: Find Second Largest Element in Array

πŸ”· C Program: Find Second Largest Element in Array

#include <stdio.h>

int main()
{
    int num;
    printf("Enter the number of elements in the array: ");
    scanf("%d", &num);

    if (num < 2)
    {
        printf("Need at least two elements to find second largest>\n");
        return 0;
    }

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

    int first, second;

    if (a[0] > a[1])
    {
        first = a[0];
        second = a[1];
    }
    else if (a[0] < a[1])
    {
        first = a[1];
        second = a[0];
    }
    else
    {
        first = second = a[0];
    }

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

    printf("The largest element is: %d\n", first);
    if (first == second)
    {
        printf("There is no second largest element (all same or repeated).\n");
    }
    else
    {
        printf("The second largest element is: %d\n", second);
    }

    return 0;
}
  

πŸ“˜ Explanation:

This program finds the second largest element in a user-provided integer array.

πŸ”Ή First, it checks whether the number of elements is at least 2, because a second largest cannot exist with fewer elements.

πŸ”Ή Then, it compares the first two elements to initialize the variables first and second.

πŸ”Ή As it iterates through the array, it keeps updating first if a larger number is found, and moves the old first into second.

πŸ”Ή It also ensures that second is not equal to first by checking a[i] != first to avoid duplicate max values.

πŸ”Ή Finally, it prints both the largest and second largest, or a message indicating no distinct second largest exists.

πŸ” Sample Output:

Enter the number of elements in the array: 5
Enter the elements of the array: 10 30 20 30 5
The largest element is: 30
The second largest element is: 20

Enter the number of elements in the array: 3
Enter the elements of the array: 40 40 40
The largest element is: 40
There is no second largest element (all same or repeated).
    

🏷️ Keywords:

second largest number in array, largest number C program, array maximum element, array sorting C, C beginner programs, C programming examples

Comments

Popular Posts

πŸŒ™