w3resource

C++ Exercises: Check if a given array of integers and length 2, contains 15 or 20

C++ Basic Algorithm: Exercise-89 with Solution

Write a C++ program to check if a given array of integers and length 2, contains 15 or 20.

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

bool test(int nums[]) {
    return nums[0] == 15 || nums[0] == 20 || nums[1] == 15 || nums[1] == 20;
    }

int main () {
  int nums1[] = { 12, 20 }; 
  int nums2[] = { 14, 15 };
  int nums3[] = { 11, 21 };
  cout << test(nums1) << endl;
  cout << test(nums2) << endl;
  cout << test(nums3) << endl;    
   return 0;
}

Sample Output:

1
1
0

Pictorial Presentation:

C++ Basic Algorithm Exercises: Check if a given array of integers and length 2, contains 15 or 20.

Flowchart:

Flowchart: Check if a given array of integers and length 2, contains 15 or 20.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ program to create a new array taking the first and last elements of a given array of integers and length 1 or more.
Next: Write a C++ program to check if a given array of integers and length 2, does not contain 15 or 20.

What is the difficulty level of this exercise?