Introduction:
When working with numbers in C programming, it’s useful to know how to isolate and analyze individual digits. One basic example is to check whether the last digit of an entered number is even or odd. In this article, you’ll learn how to write a C program to check the last digit of a number is even or odd, along with a clear explanation and output examples.
Problem Statement:
Write a C program to:
- Accept an integer input from the user.
- Determine whether the last digit of the number is even or odd.
C Program Code:
#include <stdio.h>
int main() {
int number, lastDigit;
// Input from user
printf("Enter an integer number: ");
scanf("%d", &number);
// Find last digit
lastDigit = number % 10;
// Check if even or odd
if (lastDigit % 2 == 0) {
printf("The last digit %d is Even.\n", lastDigit);
} else {
printf("The last digit %d is Odd.\n", lastDigit);
}
return 0;
}
Explanation of the Code:
- The program takes an integer input using
scanf()
. - The last digit is calculated using the modulus operator
% 10
. - The program then checks if the last digit is divisible by 2.
- If divisible, it prints “Even”; otherwise, it prints “Odd”.
Example Output:
Enter an integer number: 3457
The last digit 7 is Odd.
Enter an integer number: 1284
The last digit 4 is Even.
Conclusion:
This program provides a simple way to practice modulus operations and conditional checks in C. It is commonly used in scenarios involving digit analysis or filtering based on number characteristics.
To explore similar logic in number processing, check out our C Program to Reverse a Number article.