Print positive and negative numbers using One Dimensional Array in C


This program is written in C programming language and it does the following:

  • It first prompts the user to enter a limit 'n' which determines the size of an integer array 'a'
  • Then, it prompts the user to enter 'n' values which are stored in the array 'a'
  • Next, it prints the given values by using a for loop to iterate through the array
  • After that, it uses another for loop to check each element of the array 'a' and counts the number of positive and negative values by using an if-else statement
  • Finally, it prints the number of positive and negative values by using printf statements.
  • The program ends by returning 0 as the value of the main function.

Source Code

#include<stdio.h>
int main()
{
  int i,j,a[10],n,m,b=0,c=0;
  printf("\nEnter the limit:");
  scanf("%d",&n);
  printf("\nEnter the values:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("\nGiven values are:");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  for(i=0;i<n;i++)
  {
    if(a[i]>0)
    {
       printf("\nPositive value a[%d]=%d",i,a[i]);
       c++;
    }
    else
    {
       printf("\nNegative value a[%d]=%d",i,a[i]);
       b++;
    }
  }
  printf("\nNo of positive numbers:%d",c);
  printf("\nNo of negative numbers:%d",b);
  return 0;
}



To download raw file Click Here

Output

Enter the limit:5
Enter the values:-3
1
-6
4
-2
Given values are:
a[0]=-3
a[1]=1
a[2]=-6
a[3]=4
a[4]=-2
Negative value a[0]=-3
Positive value a[1]=1
Negative value a[2]=-6
Positive value a[3]=4
Negative value a[4]=-2

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C