C Exercises: Check whether a non-negative given number is a multiple of 3 or 7, but not both
20. Exclusive Multiple Check for 3 or 7
Write a C program that checks if a given non-negative number is a multiple of 3 or 7, but not both.
C Code:
Sample Output:
1 1 0
Explanation:
int test(int n) { return n % 3 == 0 ^ n % 7 == 0; }
The above function takes an integer n as input and returns 1 if n is divisible by 3 or 7 but not both, otherwise it returns 0. It uses the bitwise XOR operator (^) to determine if n is divisible by only one of 3 or 7, but not both.
Time complexity and space complexity:
The time complexity of the function is O(1) as it consists of a few simple operations that do not depend on the size of the input.
The space complexity of the function is O(1) as it uses a constant amount of space regardless of the size of the input.
Pictorial Presentation:
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C program to check if a number is a multiple of 2 or 5, but not both.
- Write a C program to verify if an integer is divisible by 4 or 6 exclusively.
- Write a C program that returns true if a number is divisible by either 3 or 9, but not both.
- Write a C program to determine if a given integer is divisible by either 7 or 11, exclusively.
C Programming Code Editor:
Previous: Write a C program to test whether a given non-negative number is a multiple of 13 or it is one more than a multiple of 13.
Next: Write a C program to check whether a given number is within 2 of a multiple of 10.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.