- How to input elements into an array
- How to identify and print negative elements
- How to use conditional statements inside loops
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to input elements in an array and print all negative elements.
This program is useful when you need to filter and display only the negative numbers from a given set of values. It is commonly used in:
- Data analysis where negative values need to be identified
- Financial applications to detect losses or negative transactions
- Temperature data analysis to find below-zero readings
- Sensor data processing to identify negative readings
💡 Key Point: A negative number is any number less than zero. In C, we check if a number is negative using the condition arr[i] < 0.
C Program to Print Negative Elements in Array
#include <stdio.h>
int main() {
int n, i;
int found = 0; // Flag to check if any negative element is found
// Ask user for number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Declare array of size n
int arr[n];
// Read elements into the array
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Print all negative elements
printf("\nNegative elements in the array:\n");
for(i = 0; i < n; i++) {
if(arr[i] < 0) {
printf("%d ", arr[i]);
found = 1;
}
}
// If no negative elements found
if(found == 0) {
printf("No negative elements found.\n");
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 6 Enter 6 elements: 10 -5 30 -20 40 -15 Negative elements in the array: -5 -20 -15
Another Example:
Enter the number of elements: 5 Enter 5 elements: 10 20 30 40 50 Negative elements in the array: No negative elements found.
Example with Mixed Numbers:
Enter the number of elements: 8 Enter 8 elements: -8 15 -23 7 -1 0 -45 99 Negative elements in the array: -8 -23 -1 -45
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n;— stores the number of elementsint i;— loop counterint found = 0;— a flag to check if any negative element is found (0 = not found, 1 = found)
- Get User Input: Prompts the user to enter the number of elements and then reads them into the array.
- Print Negative Elements: The
forloop iterates through each element and checks ifarr[i] < 0:- If true, it prints the element
- Sets
found = 1to indicate at least one negative number was found
- Handle No Negative Elements: If
found == 0, it prints a message indicating no negative elements were found. - Return:
return 0;indicates successful program execution.
📝 Note: Zero (0) is not considered a negative number. It is a neutral number. Only values less than zero are printed.
Algorithm to Print Negative Elements
- Start
- Read the number of elements (n)
- Read n elements into the array
- Set
found = 0 - For
i = 0ton-1:- If
arr[i] < 0:- Print
arr[i] - Set
found = 1
- Print
- If
- If
found == 0, print "No negative elements found" - End
Variation: Count Negative Elements
You can also count how many negative elements are present in the array:
#include <stdio.h>
int main() {
int n, i;
int count = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("\nNegative elements in the array:\n");
for(i = 0; i < n; i++) {
if(arr[i] < 0) {
printf("%d ", arr[i]);
count++;
}
}
if(count > 0) {
printf("\nTotal negative elements: %d\n", count);
} else {
printf("\nNo negative elements found.\n");
}
return 0;
}
💻 Practice Exercise
Challenge 1: Modify the program to print only the positive elements in the array.
Challenge 2: Print the sum of all negative elements in the array.
🔍 Click to Show Solution for Challenge 2
#include <stdio.h>
int main() {
int n, i;
int sum = 0;
int count = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("\nNegative elements in the array:\n");
for(i = 0; i < n; i++) {
if(arr[i] < 0) {
printf("%d ", arr[i]);
sum = sum + arr[i];
count++;
}
}
if(count > 0) {
printf("\nTotal negative elements: %d\n", count);
printf("Sum of negative elements: %d\n", sum);
} else {
printf("\nNo negative elements found.\n");
}
return 0;
}
/* Sample Output:
Enter the number of elements: 6
Enter 6 elements:
-5 10 -15 20 -25 30
Negative elements in the array:
-5 -15 -25
Total negative elements: 3
Sum of negative elements: -45
*/
Frequently Asked Questions
1. How do you print negative elements in an array in C?
Use a for loop to iterate through the array and check if each element is less than zero using if(arr[i] < 0). If true, print the element.
2. What is the condition to check for negative numbers?
A number is negative if it is less than zero. In C, the condition is arr[i] < 0.
3. Is zero considered a negative number?
No, zero (0) is neither positive nor negative. It is a neutral number. The condition arr[i] < 0 only prints numbers less than zero.
4. How can I count the number of negative elements?
Declare a counter variable int count = 0; and increment it each time you find a negative element using count++; inside the if condition.
5. Can I store negative elements in a separate array?
Yes, you can declare a second array and copy negative elements into it. This is useful if you need to process negative numbers separately.
💡 Tip: Using a flag variable like found helps you handle the case when no negative elements exist in the array.