C Program To Determine Divisible Even And Odd Of 2 And 3
Divisibility rules are fundamental concepts in number theory and programming, enabling efficient categorization and manipulation of integers. In this article, you will learn how to implement C programs to determine if a number is even or odd, and simultaneously check its divisibility by 2 and 3.
Problem Statement
The challenge lies in writing C code that can accurately and efficiently classify an integer based on its parity (even or odd) and its divisibility by the numbers 2 and 3. This is crucial for tasks requiring conditional logic or number filtering, where specific properties of numbers need to be identified.
Example
Consider the number 18. A program would identify it as:
- Even
- Divisible by 2
- Divisible by 3
- Even and divisible by 3
Background & Knowledge Prerequisites
To understand and implement the solutions, readers should be familiar with:
- Basic C Syntax: Variable declaration, input/output operations using
printfandscanf. - Modulo Operator (
%): This operator returns the remainder of a division. For example,10 % 3evaluates to1. - Conditional Statements:
if,else if, andelsestatements for decision-making logic. - Logical Operators:
&&(AND),||(OR) for combining multiple conditions. - Standard Input/Output Library: Including
for console operations.
Use Cases or Case Studies
Understanding how to check for divisibility by 2 and 3 has several practical applications:
- Number Filtering: In data processing, you might need to filter a list of numbers, keeping only those that are even and divisible by 3.
- Game Development: Simple games often use divisibility rules for scoring (e.g., points awarded if a number drawn is even or a multiple of 3).
- Educational Tools: Creating interactive programs to teach children about number properties and divisibility rules.
- Input Validation: Ensuring that user input meets specific numeric criteria, like requiring an even number or a multiple of three.
- Basic Cryptography/Hashing: Simple algorithms might use divisibility as part of their logic.
Solution Approaches
Here, we explore various methods to determine a number's divisibility by 2 and 3, along with its even/odd status.
Approach 1: Basic Modulo Checks for a Single Number
This approach directly applies the modulo operator to check for even/odd status and divisibility by 3 independently.
Summary: Determine if a number is even (divisible by 2) or odd, and separately check if it's divisible by 3 using the modulo operator.
// Basic Divisibility Checks
#include <stdio.h>
int main() {
int number;
// Step 1: Prompt user for input
printf("Enter an integer: ");
scanf("%d", &number);
// Step 2: Check for even or odd
if (number % 2 == 0) {
printf("%d is an even number.\\n", number);
} else {
printf("%d is an odd number.\\n", number);
}
// Step 3: Check for divisibility by 2 (redundant if even/odd checked, but explicit)
if (number % 2 == 0) {
printf("%d is divisible by 2.\\n", number);
} else {
printf("%d is not divisible by 2.\\n", number);
}
// Step 4: Check for divisibility by 3
if (number % 3 == 0) {
printf("%d is divisible by 3.\\n", number);
} else {
printf("%d is not divisible by 3.\\n", number);
}
return 0;
}
Sample Output:
Enter an integer: 18
18 is an even number.
18 is divisible by 2.
18 is divisible by 3.
Enter an integer: 7
7 is an odd number.
7 is not divisible by 2.
7 is not divisible by 3.
Stepwise Explanation:
- The program takes an integer input from the user.
number % 2 == 0checks if the remainder whennumberis divided by 2 is 0. If true, the number is even; otherwise, it's odd.number % 3 == 0checks if the remainder whennumberis divided by 3 is 0. If true, the number is divisible by 3.- Each check is performed independently using
if-elsestatements.
Approach 2: Combining Checks with Logical Operators
This approach builds on the previous one by combining the divisibility checks using logical AND (&&) to identify numbers that satisfy multiple conditions simultaneously.
Summary: Use logical && to check if a number is both even/odd and divisible by 3 in a single conditional statement.
// Combined Divisibility Checks
#include <stdio.h>
int main() {
int number;
// Step 1: Prompt user for input
printf("Enter an integer: ");
scanf("%d", &number);
// Step 2: Check for even and divisible by 3
if (number % 2 == 0 && number % 3 == 0) {
printf("%d is an even number and is divisible by 3.\\n", number);
}
// Step 3: Check for odd and divisible by 3
else if (number % 2 != 0 && number % 3 == 0) {
printf("%d is an odd number and is divisible by 3.\\n", number);
}
// Step 4: Check for even but not divisible by 3
else if (number % 2 == 0 && number % 3 != 0) {
printf("%d is an even number but not divisible by 3.\\n", number);
}
// Step 5: Check for odd and not divisible by 3
else { // This covers number % 2 != 0 && number % 3 != 0
printf("%d is an odd number and not divisible by 3.\\n", number);
}
return 0;
}
Sample Output:
Enter an integer: 6
6 is an even number and is divisible by 3.
Enter an integer: 9
9 is an odd number and is divisible by 3.
Enter an integer: 10
10 is an even number but not divisible by 3.
Enter an integer: 7
7 is an odd number and not divisible by 3.
Stepwise Explanation:
- The program takes an integer input.
- It uses a series of
if-else if-elsestatements to categorize the number. - Each
iforelse ifcondition combines two modulo checks using the logical AND operator (&&).
-
number % 2 == 0 && number % 3 == 0: True if the number is even AND divisible by 3. -
number % 2 != 0 && number % 3 == 0: True if the number is odd AND divisible by 3. -
number % 2 == 0 && number % 3 != 0: True if the number is even AND not divisible by 3.
- The final
elseblock catches all other cases, which logically must be odd and not divisible by 3.
Approach 3: Using Functions for Reusability
For larger programs or when these checks need to be performed multiple times, encapsulating the logic within functions promotes reusability and improves code readability.
Summary: Define separate functions for checking even, odd, divisible by 2, and divisible by 3 to modularize the code.
// 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;
}
Sample Output:
Enter an integer: 12
12 is an even number.
12 is divisible by 3.
12 is even and divisible by 3.
Enter an integer: 5
5 is an odd number.
5 is not divisible by 3.
5 is odd and not divisible by 3.
Stepwise Explanation:
- Three functions,
isEven,isOdd, andisDivisibleBy3, are defined. Each takes an integer and returnstrueorfalse(from) based on its respective check. - In
main, after getting user input, these functions are called to determine the number's properties. - The
if-else if-elsestructure then uses the return values of these functions, combined with logical operators, to categorize the number comprehensively. - This approach makes the
mainfunction cleaner and easier to read, as the detailed logic for each check is abstracted into dedicated functions.
Conclusion
Determining a number's parity and divisibility by specific integers like 2 and 3 are foundational skills in C programming. By mastering the modulo operator and conditional statements, whether through direct checks, combined logical operations, or modular functions, you can effectively categorize and process numbers according to these essential properties.
Summary
- The modulo operator (
%) is key for checking divisibility; a remainder of 0 indicates divisibility. - A number is even if
number % 2 == 0; otherwise, it's odd. - Logical operators (
&&,||) enable combining multiple divisibility conditions efficiently. - Functions enhance code organization and reusability for repeated checks, making programs cleaner and more maintainable.
- These checks are vital for conditional logic, data filtering, and input validation in various applications.