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; ...

Linear Search in an Array - C Program

Linear Search in Array - C Program

✅ Linear Search in an Array - C Program

#include <stdio.h>

int main() {
    int size, key, found = 0;

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

    if (size <= 0) {
        printf("Invalid array size!\n");
        return 1;
    }

    int arr[size];

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

    // Input the element to search
    printf("Enter the element to search:\n");
    scanf("%d", &key);

    // Search for the element
    for (int i = 0; i < size; i++) {
        if (arr[i] == key) {
            printf("Element %d found at index %d (position %d)\n", key, i, i + 1);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Element %d not found in the array.\n", key);
    }

    return 0;
}
  

πŸ“˜ Explanation:

This program demonstrates a simple linear search in an array. It takes the array size and elements from the user, along with the target value to search. It then loops through the array and checks for equality with the given key. If found, it prints the index and position. If the element is not present, it informs the user accordingly. The `break` statement is used to stop at the first occurrence.

🧾 Sample Output:

Enter the size of the array:
5
Enter 5 elements:
10 20 30 40 50
Enter the element to search:
30
Element 30 found at index 2 (position 3)
  

πŸ”‘ Keywords:

Array Search in C, Linear Search Program, Find Index of Element, C Array Program, Beginner C Code, Searching in Arrays

πŸ“Œ Hashtags:

#CProgramming #ArraySearch #LinearSearch #BeginnerC #CCodeWithExplanation #DataSearch

Comments

Popular Posts

πŸŒ™