Saturday, 31 October 2015

C++ Program For Count Positive And Negative Number in An Array

Problem:- Program to count Number of the Positive and negative number in a 1-D integer Array. Means you have to count a positive and negative number in an Array

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Logic:- For this problem simple fact you have to know that if the number is greater than zero then the number is positive if the number is less than the number is negative otherwise the number is zero. So for this problem, we have two variable one for positive number and another for the negative number and as I said above we compare the number with zero or if the number is greater than zero we will increase the positive variable by one for negative increase negative variable by one. Before solving follow C Program To Check Number Is Positive Or Negative

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post.


Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Solution:-

#include<iostream>
using namespace std;
int main()
{
  int a[100],i,n,zero=0,pos=0,neg=0;
 
cout<<"Enter The Size of An Array :\n";
  cin>>n;

  cout<<"Enter The Element :\n";
  for(i=0;i<n;i++)
  {
  cin>>a[i];
  }

  cout<<"Elment in Array is Given Below\n";
  for(i=0;i<n;i++)
  {
  if(a[i]>0)
  pos++;
  else if(a[i]<0)
  neg++;
  else
  zero++;
  }
  cout<<"\nPositive No. is = "<<pos;
  cout<<"\nNegative No. is = "<<neg;
  cout<<"\nTotal Zero in array is = "<<zero;
 return 0;

}

Output:-

C++ Program For Count Positive And Negative Number in An Array


You May Also See


1. C++ Program To Merge One Accending And Another One Is Descending Array In Third Array Should be Descending Order



Ghanendra Yadav
Ghanendra Yadav

I Am Ghanendra Yadav Owner Of This Website. I Love Programming, Web Developing, Logo Making, Banner And Website designing. Currently I Am Studying In NIT Warangal And Also Working With WSDC( As Web Application Developer ) In NIT Warangal For Any Query Mail Me I Will Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

5 comments:

  1. Below there is an Array of positive and negative number. Write a program that calculates how many of the numbers are positive and how many of them are negative. A [ ] = {10, -2, 9. -4, -6, -7, 12, 14, 19, -20}

    ReplyDelete
  2. You are askig same Query. I think you are searching for C++ Program To Check Number Is Positive Or Negative If you have any Other Query send a Email.

    Your Solution Is

    /*
    Below there is an Array of positive and negative number. Write a program that calculates how many of
    the numbers are positive and how many of them are negative. A [ ] = {10, -2, 9. -4, -6, -7, 12, 14, 19, -20} on
    */

    #include
    #include

    using namespace std;

    int main()
    {
    int n, i,countneg = 0 , countpos = 0, countzero = 0;

    cout<<"Enter The Array Size: ";
    cin>>n;

    int arr[n];

    cout<<"\nEnter The Array Elements: \n";

    for(i = 0; i < n; i++)
    {
    cin>>arr[i];
    }

    for(i = 0; i < n; i++)
    {
    if(arr[i]<0)
    {
    countneg++;
    }
    else if(arr[i]>0)
    {
    countpos++;
    }
    else
    {
    countzero++;
    }
    }

    cout<<"\n\nTotal Number of Positive Elements in An Array is: "<<countpos;
    cout<<"\nTotal Number of Negative Elements in An Array is: "<<countneg;
    cout<<"\nTotal Number of Zero Elements in An Array is: "<<countzero;

    return 0;
    }

    ReplyDelete
    Replies
    1. Header files are

      #include&ltiostream&gt
      #include&ltcstring&gt

      Delete
    2. I don't know Why header files are not working....

      Delete
    3. Hello, Sahni

      we know that in Header file we use less than and greater than symbol, But this page compile the whole page except page body that is why header files are not showing. So don't worry about header file everybody knows header files in C/C++.

      Delete