Write C++ program to count total number of negative elements in array


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #define MAX_SIZE 100 //Maximum size of the array
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int arr[100]; //Declaring size of an array as 100
  8. int i, num, count=0;
  9.  
  10. //Reads size and elements of array
  11.  
  12. cout<<"Enter size of the array : ";
  13. cin>>num;
  14.  
  15. cout<<"Enter elements in array : ";
  16. for(i=0; i<num; i++)
  17. {
  18. cin>>arr[i];
  19. }
  20.  
  21. //Counts total number of negative elements
  22. for(i=0; i<num; i++)
  23. {
  24. if(arr[i]<0)
  25. {
  26. count++; //couting negative elements
  27. }
  28. }
  29. cout<<"Total number of negative elements: "<<count;
  30.  
  31. return 0;
  32. }
  33.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts