C Program to Print Positive Numbers in an Array

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

C Program to Print Positive 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 Positive Numbers

/* C Program to Print Positive 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 Positive Numbers in this Array  :  "); 
 for(i = 0; i < Size; i ++)
 {
   if(a[i] >= 0)
   {
	   	printf("%d  ", a[i]);
   }
 }
 return 0;
}
C Program to Print Positive 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 4

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

In the Next line, We declared the If statement

if(a[i] >= 0)

Any number that is greater than or equal to 0 is a Positive Number. Condition inside the If statement will check for the same. If the condition is True, then it is a Positive Number, and the C Programming compiler will print those values.

Program to Print Positive Numbers in an Array using While Loop

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

/* C Program to Print Positive 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 Positive Numbers in this Array  :  "); 
 while(j < Size)
 {
   if(a[j] >= 0)
   {
	   	printf("%d  ", a[j]);
   }
   j++;
 }
 return 0;
}

C Positive Numbers in an Array using a while loop output

Please Enter the Size of an Array :  8

 Please Enter the Array Elements  :  12 -25 -89 19 -22 48 79 125

 List of Positive Numbers in this Array  :  12  19  48  79  125

Program to Print Positive Numbers in an Array using Functions

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

/* C Program to Print Positive Numbers in an Array */

#include<stdio.h>

void PrintPositiveNumbers(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]);
 }
 
 PrintPositiveNumbers(a, Size);
 return 0;
}

void PrintPositiveNumbers(int a[], int Size)
{
	int i;
	printf("\n List of Positive 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  :  -12 14 -56 98 16 -78 105 569 -3 100

 List of Positive Numbers in this Array  :  14 	 98 	 16 	 105 	 569 	 100