C Program to Print Negative Numbers in an Array

How to write a C Program to Print Negative Numbers in an Array using For Loop, While Loop, and Functions with example.

C Program to Print Negative Numbers in an Array

This program allows the user to enter the Size and the row elements of One Dimensional Array. Next, we are using For Loop to iterate the array values and find Negative Numbers in C.

/* C Program to Print Negative Numbers in an Array */
#include<stdio.h>
 
int main()
{
 int Size, i, a[10];
 
 printf("\n Please Enter the Size of an Array :  ");
 scanf("%d", &Size);
 
 printf("\n Please Enter the Array Elements  :  ");
 for(i = 0; i < Size; i++)
 {
      scanf("%d", &a[i]);
 }
 
 printf("\n List of Negative Numbers in this Array  :  "); 
 for(i = 0; i < Size; i ++)
 {
   if(a[i] < 0)
   {
	   	printf("%d  ", a[i]);
   }
 }
 return 0;
}
C Program to Print Negative Numbers in an Array 1

Here, For Loop will make sure that the number is between 0 and maximum One Dimensional Array size value. In this example, it will be from 0 to 7

for(i = 0; i < Size; i ++)

In the next line, We declared the If statement

if(a[i] >= 0)

Any number that is less than 0 is a Negative Number. Condition inside the If statement will check for the same. If the condition is True, it is a Negative Number, and the C Programming compiler will print those values.

Program to Print Negative Numbers in an Array using While Loop

This program is the same as above, but this time we used While Loop to print Negative Numbers in an array

/* C Program to Print Negative Numbers in an Array */
#include<stdio.h>
 
int main()
{
 int Size, i, j = 0, a[10];
 
 printf("\n Please Enter the Size of an Array :  ");
 scanf("%d", &Size);
 
 printf("\n Please Enter the Array Elements  :  ");
 for(i = 0; i < Size; i++)
 {
      scanf("%d", &a[i]);
 }
 
 printf("\n List of Negative Numbers in this Array  :  "); 
 while(j < Size)
 {
   if(a[j] < 0)
   {
	   	printf("%d  ", a[j]);
   }
   j++;
 }
 return 0;
}
Please Enter the Size of an Array :  5

 Please Enter the Array Elements  :  -12 59 -89 28 -16

 List of Negative Numbers in this Array  :  -12  -89  -16  

Program to Print Negative Numbers in an Array using Functions

This program to print negative numbers in an array is the same as the first example. But we separated the logic to print negative numbers in an array using Functions.

/* C Program to Print Negative Numbers in an Array */
#include<stdio.h>
void PrintNegativeNumbers(int a[], int Size);
int main()
{
 int Size, i, a[10];
 
 printf("\n Please Enter the Size of an Array :  ");
 scanf("%d", &Size);
 
 printf("\n Please Enter the Array Elements  :  ");
 for(i = 0; i < Size; i++)
 {
      scanf("%d", &a[i]);
 }
 
 PrintNegativeNumbers(a, Size);
 return 0;
}

void PrintNegativeNumbers(int a[], int Size)
{
	int i;
	printf("\n List of Negative Numbers in this Array  :  "); 		
 	for(i = 0; i < Size; i++)
  	{
  		if(a[i] < 0)
  		{
  			printf("%d \t ", a[i]);
		}
    }
}
Please Enter the Size of an Array :  10

 Please Enter the Array Elements  :  -15 -89 22 19 -56 88 97 -59 -105 179

 List of Negative Numbers in this Array  :  -15   -89 	 -56 	 -59 	 -105