Learn More Way

Problem :

Print All Negative Elements in an Array

Print All Negative Elements in an Array

যা শিখবো:

ধাপে ধাপে ব্যাখ্যা:

Step 1: ইউজার থেকে array size ইনপুট নেওয়া

C
scanf("%d", &n);

Step 2: ইউজার থেকে array elements ইনপুট নেওয়া

C
for(i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
}

Step 3: negative elements চেক করা এবং প্রিন্ট করা

C
for(i = 0; i < n; i++) {
    if(arr[i] < 0) {
        printf("%d ", arr[i]);
    }
}

উদাহরণ:

C
#include <stdio.h>

int main() {
    int n, i;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    int arr[n];

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

    printf("\nNegative elements in the array: ");
    for(i = 0; i < n; i++) {
        if(arr[i] < 0) {
            printf("%d ", arr[i]);
        }
    }

    return 0;
}

আউটপুট উদাহরণ:

উদাহরণ ১:

C
Enter the number of elements: 6
Enter the elements:
5 -3 0 7 -1 9

Negative elements in the array: -3 -1

উদাহরণ ২:

C
Enter the number of elements: 4
Enter the elements:
10 20 30 40

Negative elements in the array:

(কোনো negative element নেই)

ব্যাখ্যা:

©2025 Linux Bangla | Developed & Maintaind by Linux Bangla.