Write C++ program to print all negative elements in an 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[MAX_SIZE]; //Declares an array size
  8. int i, num;
  9.  
  10. //Enter size of array
  11. cout<<"Enter size of the array: ";
  12. cin>>num;
  13.  
  14. //Reading elements of array
  15. cout<<"Enter elements in array: ";
  16. for(i=0; i<num; i++)
  17. {
  18. cin>>arr[i];
  19. }
  20.  
  21. cout<<"All negative elements in array are:";
  22. for(i=0; i<num; i++)
  23. {
  24. //Printing negative elements
  25. if(arr[i] < 0)
  26. {
  27. cout<<arr[i];
  28. }
  29. }
  30.  
  31. return 0;
  32. }
  33.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts