w3resource

C Exercises: Check whether two given integer values are in the range 20..50 inclusive

C-programming basic algorithm: Exercise-9 with Solution

Write a C program that checks if three given integers are in the range of 20 to 50 (inclusive) and returns true if at least one of them is within the range. If none of the integers are within the range, the program returns false.

C Code:

#include <stdio.h>  // Include standard input/output library
#include <stdlib.h> // Include standard library for additional functions

// Declare the function 'test' with two integer parameters
int test(int x, int y);

int main(void)
{
    // Call the function 'test' with arguments 20 and 84, and print the result
    printf("%d", test(20, 84));

    // Print a newline for formatting
    printf("\n");

    // Call the function 'test' with arguments 14 and 50, and print the result
    printf("%d", test(14, 50));

    // Print a newline for formatting
    printf("\n");

    // Call the function 'test' with arguments 11 and 45, and print the result
    printf("%d", test(11, 45));

    // Print a newline for formatting
    printf("\n");

    // Call the function 'test' with arguments 25 and 40, and print the result
    printf("%d", test(25, 40));
}

// Function definition for 'test'
int test(int x, int y)
{
    // Check if either of the following conditions are true:
    // 1. x is less than or equal to 20 OR y is greater than or equal to 50
    // 2. x is less than or equal to 20 OR y is greater than or equal to 50
    // If any of the conditions are true, return 1 (true). Otherwise, return 0 (false).
    return (x <= 20 || y >= 50) || (x <= 20 || y >= 50);
}

Sample Output:

1
1
1
0

Explanation:

int test(int x, int y) {
  return (x <= 20 || y >= 50) || (x <= 20 || y >= 50);
}

The above function takes two integer arguments ‘x’ and ‘y’ and returns 1 if either ‘x’ is less than or equal to 20 or ‘y’ is greater than or equal to 50, otherwise it returns 0.

Time complexity and space complexity:

Time complexity of the function is O(1) as it contains only constant-time operations and the execution time does not depend on the input size.

Space complexity of the function is O(1) as the function uses only a few variables of fixed size regardless of the input size.

Pictorial Presentation:

C Programming Algorithm: Check whether two given integer values are in the range 20..50 inclusive

Flowchart:

C Programming Algorithm Flowchart: Check whether two given integer values are in the range 20..50 inclusive

C Programming Code Editor:

Previous: Write a C program to compute the check whether three given integer values are in the range 20..50 inclusive. Return true if 1 or more of them are in the said range otherwise return false.
Next: Write a C program to check which number nearest to the value 100 among two given integers. Return 0 if the two numbers are equal.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.