C Online Compiler
Example: Functional Divisibility Checks in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Functional Divisibility Checks #include <stdio.h> #include <stdbool.h> // For using bool type // Function to check if a number is even bool isEven(int num) { return (num % 2 == 0); } // Function to check if a number is odd bool isOdd(int num) { return (num % 2 != 0); } // Function to check if a number is divisible by 3 bool isDivisibleBy3(int num) { return (num % 3 == 0); } int main() { int number; // Step 1: Prompt user for input printf("Enter an integer: "); scanf("%d", &number); // Step 2: Use functions to determine properties if (isEven(number)) { printf("%d is an even number.\n", number); } else { printf("%d is an odd number.\n", number); } if (isDivisibleBy3(number)) { printf("%d is divisible by 3.\n", number); } else { printf("%d is not divisible by 3.\n", number); } // Step 3: Combine functional checks if (isEven(number) && isDivisibleBy3(number)) { printf("%d is even and divisible by 3.\n", number); } else if (isOdd(number) && isDivisibleBy3(number)) { printf("%d is odd and divisible by 3.\n", number); } else if (isEven(number) && !isDivisibleBy3(number)) { printf("%d is even but not divisible by 3.\n", number); } else { // isOdd(number) && !isDivisibleBy3(number) printf("%d is odd and not divisible by 3.\n", number); } return 0; }
Output
Clear
ADVERTISEMENTS