GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 384 of 453    Print  
C program to count numbers of positive and negative numbers
Write a C program using arrays to count the numbers of positive and negative numbers which accepts the inputs as "size of the array" & "elements of the array" and outputs as "number of negative numbers in an array are" & "numbers of positive numbers in an array are" ?


  
Total Answers and Comments: 5 Last Update: July 12, 2009     Asked by: Tai 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
October 31, 2007 05:20:19   #1  
baseersd Member Since: June 2007   Contribution: 34    

RE: C program to count numbers of positive and negativ...
#include <stdio.h>
int main()
{
int p_nArr[20] {0};
int nCount_Pos 0;
int nCount_Neg 0;
int num 0;
int SIZEOFARRAY 0;
int index;
printf("nEnter the Size of the array");
scanf(" d" &SIZEOFARRAY);
printf("nEnter the elements ::n");
for( index 0; index < SIZEOFARRAY; index++ )
{
scanf(" d" &p_nArr[index]);
if (p_nArr[index] > 0 )
nCount_Pos++ ;
else if (p_nArr[index] < 0 )
nCount_Neg++;
}
if ( nCount_Neg > 0)
{
printf("nNumber of negative numbers in the array are :: dn" nCount_Neg);
for( index 0; index < SIZEOFARRAY; index++ )
{
if (p_nArr[index] < 0 )
printf(" d " p_nArr[index]);
}
}
if ( nCount_Pos > 0 )
{
printf("nNumber of Positive numbers in the array are :: dn" nCount_Pos);
for( index 0; index < SIZEOFARRAY; index++ )
{
if (p_nArr[index] > 0 )
printf(" d " p_nArr[index]);
}
}

getch();
}

//Note : This program can be solved efficiently using linked list.

 
Is this answer useful? Yes | No
December 27, 2007 12:05:00   #2  
kaushalgoa Member Since: December 2007   Contribution: 7    

RE: C program to count numbers of positive and negative numbers
void func(int num int * arr int *pos int *neg)
{
int i 0;
*pos 0;
*neg 0;
for(i 0; i < num; i++)
{
if(*(arr + i) > 0)
(*pos)++;
else
(*neg)++;
}
return;
}

void main()
{

int array[] {1 2 3 4 5 -4 -6 -8 0};
int pos neg;
func(9 array &pos &neg);
return;

}

 
Is this answer useful? Yes | No
June 18, 2008 04:30:09   #3  
jintojos Member Since: May 2008   Contribution: 29    

RE: C program to count numbers of positive and negative numbers
#include<stdio.h>
#include<conio.h>

void main()
{
int *arr num i;
clrscr();
printf("Enter The Number Of Elements :");
scanf(" d" &num);
arr (int *)malloc(num*2);
printf("Enter The numbers :");
for(i 0;i<num;i++) scanf(" d" (arr+i));
printf("nPositive Numbers Are....n");
for(i 0;i<num;i++) if(arr[i]>0) printf(" d" arr[i]);
printf("nNegative Numbers Are....n");
for(i 0;i<num;i++) if(arr[i]<0) printf(" d" arr[i]);
getch();
}

 
Is this answer useful? Yes | No
June 18, 2008 04:36:04   #4  
jintojos Member Since: May 2008   Contribution: 29    

RE: C program to count numbers of positive and negative numbers
#include<conio.h>
#include<stdio.h>

void main()
{
int *arr num i 0 pos 0 neg 0;
clrscr();
printf("Enter The Number Of Elements :");
scanf(" d" &num);
arr (int *)malloc(num*2);
printf("Enter The Numbers :");
for(i 0;i<num;i++)
{
scanf(" d" arr+i);
if(arr[i]>0) pos++;
else neg++;
}
printf("The Number Of Positive Numbers Are : d" pos);
printf("The Number Of Negative Numbers Are : d" neg);
printf("Number Of Zeros Are : d" num-(pos+neg));
getch();
}

 
Is this answer useful? Yes | No
July 12, 2009 22:16:47   #5  
kesineni Member Since: January 2006   Contribution: 1    

RE: C program to count numbers of positive and negative numbers
#include <stdio.h>

main()
{
int a[] { 1 -3 -5 -4 -9 2 8};
int len i val;
int npos 0 nneg 0;

len sizeof(a)/sizeof(a[0]);
printf(" dn" len);

for( i 0; i< len; i++)
{
val a[i] & 0x10000000;
printf(" dn" val);
if (val > 0)
nneg++;
else
npos++;
}

printf("npos d nneg dn" npos nneg);

}

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape