Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

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

๐ŸŒ™