C Exercises: Compute the sum of the two specified integers. If one of the given integer value is in the range 10..20 inclusive return 18
C-programming basic algorithm: Exercise-22 with Solution
Write a C program to compute the sum of the two given integers. Return 18 if one of the integer values given is in the range 10..20 inclusive.
C Code:
#include <stdio.h>
#include <stdlib.h>
int main(void){
printf("%d",test(3, 7));
printf("\n%d",test(10, 11));
printf("\n%d",test(10, 20));
printf("\n%d",test(21, 220));
}
int test(int x, int y)
{
return (x >= 10 && x <= 20) || (y >= 10 && y <= 20) ? 18 : x + y;
}
Sample Output:
10 18 18 241
Explanation:
int test(int x, int y) { return (x >= 10 && x <= 20) || (y >= 10 && y <= 20) ? 18 : x + y; }
The function takes two integer arguments x and y, and returns 18 if either x or y is between 10 and 20 (inclusive). Otherwise, it returns the sum of x and y.
Time complexity and space complexity:
The time complexity of this function is O(1) because it only performs a constant number of operations, regardless of the input size.
The space complexity of this function is O(1) because it only uses a constant amount of memory to store the input arguments and the return value.
Pictorial Presentation:
Flowchart:

C Programming Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C program to check if a given number is within 2 of a multiple of 10.
Next: Write a C program to check if it is possible to add two integers to get the third integer from three given integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
What does (x ^ 0x1) != 0 mean?
The XOR operation (x ^ 0x1) inverts bit 0. So the expression effectively means: if bit 0 of x is 0, or any other bit of x is 1, then the expression is true.
Conversely the expression is false if x == 1.
So the test is the same as:
if (x != 1)
and is therefore (arguably) unnecessarily obfuscated.
Ref :https://bit.ly/2NIisQM
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook