Write a C Program to Check Whether Number is Even or Odd.
In this Article of programming, simplicity often lays the foundation for understanding more complex concepts. The task of determining whether a number is even or odd might seem elementary, but it serves as an excellent starting point for beginners to get acquainted with the fundamentals of programming in C. In this article, we will walk you through the process of creating a C program to check whether a number is even or odd, all without the use of structures or functions.
Here's a C Program to Check Whether Number is Even or Odd:
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
// Input
printf("Enter an integer: ");
scanf("%d", &num);
// Check if even or odd
if (num % 2 == 0)
{
printf("%d is an even number.\n", num);
}
else
{
printf("%d is an odd number.\n", num);
}
}
#include<conio.h>
void main()
{
int num;
// Input
printf("Enter an integer: ");
scanf("%d", &num);
// Check if even or odd
if (num % 2 == 0)
{
printf("%d is an even number.\n", num);
}
else
{
printf("%d is an odd number.\n", num);
}
}
- we declare an integer variable named "num" to hold the input value
- we prompt the user to input a number and use the "scanf" function to read and store the input in the "num" variable.
-
Check Number is Even or Odd
if (num % 2 == 0) {
Using the if-else statements,
printf("%d is an even number.\n", num);
}
else
{
printf("%d is an odd number.\n", num);
}
- we find modular of num by 2.
- If it's come zero, then given number by user is Even.
- Otherwise, then given number by user is Odd.