Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

Find Nth Largest Number in an Array (C Program)

Find Nth Largest Number in an Array (C Program)

✅ C Program to Find the Nth Largest Number in an Array

#include <stdio.h>

// Function to sort array in descending order
void sortDescending(int arr[], int size) {
    for(int i = 0; i < size-1; i++) {
        for(int j = i+1; j < size; j++) {
            if(arr[i] < arr[j]) {
                // Swap
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

int main() {
    int arr[100], n, size;

    printf("Enter the size of array: ");
    scanf("%d", &size);

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

    printf("Enter the value of n (e.g., 1st, 2nd largest): ");
    scanf("%d", &n);

    if(n <= 0 || n > size) {
        printf("Invalid value of n!\n");
        return 1;
    }

    // Sort the array in descending order
    sortDescending(arr, size);

    printf("The %dth largest number is: %d\n", n, arr[n - 1]);

    return 0;
}
  

πŸ“˜ Explanation:

This program accepts an array of integers from the user and then finds the nth largest number. It sorts the array in descending order using a simple selection sort logic, then returns the (n - 1)th index element as the nth largest.

  • sortDescending() is a helper function that arranges the elements in descending order.
  • User inputs the size of the array, the array elements, and the value of n.
  • Program validates n to ensure it's within the range.
  • It returns the required element after sorting.

🧾 Sample Output:

Enter the size of array: 5
Enter 5 elements:
10 50 30 20 40
Enter the value of n (e.g., 1st, 2nd largest): 3
The 3th largest number is: 30
  

πŸ”‘ Keywords:

nth largest number, sort array in C, array logic, sorting algorithms, find max values, C interview programs, AdSense code snippets

πŸ“Œ Hashtags:

#CProgramming #ArraySorting #NthLargest #InterviewQuestion #BeginnerFriendly #AdSenseCCode #CodeWithLogic

πŸ” Search Description:

This C program finds the nth largest number from a given array using sorting. It sorts the array in descending order and returns the required position element. Easy to understand and ideal for learning array manipulation logic.

Comments

Popular Posts

πŸŒ™