Advertisement
janac

Display non-zero elements

Nov 17th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Print the non-zero elements of an array.
  5. int main()
  6. {
  7.     // Create a list of numbers.
  8.     int numbers[]{ 1, 0, 3, 4, 0, 6, 7, 8, 9};
  9.  
  10.     // Check each number.
  11.     for (int n : numbers)
  12.     {
  13.         // If the number is not zero,
  14.         if (n != 0)
  15.         {
  16.             // Display the number.
  17.             cout << n << ' ';
  18.         }  
  19.     }
  20.     // Put the cursor on the next line.
  21.     cout << endl;
  22.  
  23.     // End the program.
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement