C Program to Check Whether a Number is Even or Odd
Here’s a C program to check whether a number is even or odd:
#include <stdio.h>
int main() {
int number;
// Step 2: Read an integer from the user
printf("Enter an integer: ");
scanf("%d", &number);
// Step 3: Check if the number is even or odd
if (number % 2 == 0) {
// Step 4: Output if the number is even
printf("%d is an even number.\n", number);
} else {
// Step 4: Output if the number is odd
printf("%d is an odd number.\n", number);
}
// Step 5: End
return 0;
}
Explanation:
Include Header: #include <stdio.h> is used to include the standard input-output library in C, which allows the use of printf and scanf.
Main Function: The main
function is where the program starts execution.
Variable Declaration: An integer variable number
is declared to store the user input.
Reading Input:
- printf(“Enter an integer: “); prompts the user to enter a number.
- scanf(“%d”, &number); reads the integer input from the user and stores it in the variable number.
Conditional Check:
- if (number % 2 == 0) checks if the remainder when number is divided by 2 is zero. If true, the number is even.
- else handles the case when the remainder is not zero, meaning the number is odd.
Output:
- printf(“%d is an even number.\n”, number); prints the message that the number is even if the condition in the if statement is true.
- printf(“%d is an odd number.\n”, number); prints the message that the number is odd if the condition in the if statement is false.
Return Statement: return 0; indicates that the program ended successfully.
Algorithm:
Step 1: Start
Step 2: Input: Read an integer n from the user.
Step 3: Process:
If n % 2 == 0 then:
The number is even.
Else:
The number is odd.
Step 4: Output: Display whether the number is even or odd.
Step 5: End
Start
- This step indicates the beginning of the algorithm. It signifies the point at which the algorithm starts its execution.
Input: Read an integer n from the user.
- In this step, the algorithm waits for the user to input an integer value. This integer is stored in a variable n.
- This step is crucial because the algorithm needs a number to work with, and it can’t proceed without this input.
Process:
- This step involves the actual logic to determine if the number is even or odd.
- The algorithm uses the modulus operator % to check the remainder of the division of n by 2.
- Sub-step: If n % 2 == 0 then:
- This sub-step checks if the remainder when n is divided by 2 is zero.
- If n % 2 == 0 evaluates to
true
, it means n is divisible by 2 with no remainder, hence n is even.
Output: Display whether the number is even or odd.
- Based on the result of the check in step 3, the algorithm outputs a message indicating whether n is even or odd.
- If the condition in step 3 is true, it outputs that the number is even.
- If the condition in step 3 is false, it outputs that the number is odd.
End
- This step signifies the termination of the algorithm. It indicates that the algorithm has completed its execution.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.