w3resource

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

C++ Basic Algorithm: Exercise-89 with Solution

Check 15 or 20 in Array (Length 2)

Write a C++ program to determine whether a given array of integers of length 2 contains 15 or 20.

Sample Solution:

C++ Code :

#include <iostream> // Including the input/output stream library
using namespace std; // Using the standard namespace

// Function that checks if any of the first two elements in the array are equal to 15 or 20
bool test(int nums[]) {
    return nums[0] == 15 || nums[0] == 20 || nums[1] == 15 || nums[1] == 20;
}

// Main function
int main () {
    int nums1[] = { 12, 20 }; // Define an array nums1
    int nums2[] = { 14, 15 }; // Define an array nums2
    int nums3[] = { 11, 21 }; // Define an array nums3

    // Output the result of test function for nums1, nums2, and nums3 arrays
    cout << test(nums1) << endl;
    cout << test(nums2) << endl;
    cout << test(nums3) << endl;    

    return 0; // Return statement indicating successful termination of the program
}

Sample Output:

1
1
0

Visual 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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/cpp-exercises/basic-algorithm/cpp-basic-algorithm-exercise-89.php