C Program Checker for Even or Odd Integer
Introduction
The C programming language is a procedural programming language. Between 1969 and 1973, Dennis Ritchie worked on the project. It was created primarily as a system programming language for developing operating systems.
In order to understand the code given below you’ve got to have prior knowledge and or understanding of the following topics:
If you have a sound knowledge and understanding of what the functions of the concepts are then there will be no difficulty in understanding the given codes below. With all that said lets get coding!
Why Learn C Programming Language?
Because it mixes the characteristics of high-level languages with the functionalism of assembly language, C is often referred to as a middle-level computer language. The manipulation of bits, bytes, and addresses in C gives the programmer more control over how the program will behave and more direct access to the underlying hardware mechanisms.
Working programmers influenced, influenced, and field-tested C. As a result, C provides the programmer with exactly what he or she want. C++ is a more advanced version of the C programming language. C++ incorporates all of the features of C, as well as support for object-oriented programming (OOP). Furthermore, C++ includes numerous enhancements and features that make it a “better C,” regardless of whether or not it is used for object-oriented programming.
Even / Odd Checker in C
Even numbers are those that are perfectly divisible by two otherwise, they are odd.
There are two methods for determining whether a number is even or odd:
Method 1: Using Bitwise Operator
Method 2: Using Modulus Operatro (%), dividing number by 2. Then using if…else statements.
Bitwise operators can be used in the C programming language to execute operations on a bit level. Bitwise operations are compared with byte-level operations, which are the logical counterparts of the bitwise operators, the AND, OR, and NOT operators.
Here in C program the semantics use for the bitwise operators are:
a) In C or C++, the bitwise (&) AND operator accepts two numbers as operands and performs AND on each bit of the two numbers. Only if both bits are 1 is the outcome of AND.
b) In C or C++, the bitwise ( | ) OR operator accepts two numbers as operands and performs OR on each bit of the two numbers. If any of the two bits is 1, the result of OR is 1.
c) In C or C++, the bitwise XOR (^) operator accepts two numbers as operands and performs XOR on each bit of the two numbers. If the two bits are different, the result of XOR is 1.
d) The left shift (<<) operator in C or C++ accepts two numbers, left shifts the bits of the first operand, and the number of places to shift is determined by the second operand.
e) The right shift (>>) operator in C or C++ takes two numbers, right shifts the bits of the first operand, and the number of places to shift is determined by the second operand.
f) In C or C++, the bitwise NOT operator (~) inverts all bits of an integer.
- Here is an example
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int num=11;
if(num%2==0)
{
printf("Given Number is eveno num %d",num);
}
else
{
printf("Given Number is odd num %d",num);
}
}
The best way to learn is to do. Hence, what better way to learn C programming language (if you know the basics) than to code your first mini project. In this tutorial we are going to build a even or odd checker in c programming language. Keep in mind this is a mini project for absolute beginners and intermediate programmers as well.
Method 1: Using Bit wise Operator
Using the bitwise operator, check if anything is even or odd.
Least Significant Bit of an odd number is always chosen as (1). To determine whether a number is even or odd, we must first determine whether the LSB is set.Using the bitwise operator is even more unusual.
To determine if a bit is set or not, we utilize the Bitwise AND & operator. Similarly, if you type num & 1, it will return the LSB of num. If the LSB value is 1, the provided integer is odd; otherwise, it is even.
// C program to check whether a number is even or odd using bitwise operator
// Method 1
#include <stdio.h> // Our header files
#include <stdlib.h>
#include <conio.h>
int main()
{
int num;
// Input number from user
printf("Enter Your number to check your even or odd: ");
scanf("%d", &num);
(num & 1)
? printf("%d is odd.", num)
: printf("%d is even.", num);
return 0;
}
Method 2: Using if…else Statements
Get input from the keyboard (user) and see if the number is even or odd.
When a user enters a number, we determine whether it is even or odd. It’s an even number if the input integer is totally divisible by 2, otherwise it’s an odd number.
// C program to check weather the given positive integer is even or odd
// Even or Odd Checker in C using method 2
#include <stdio.h> // Header files
#include <stdlib.h>
#include <conio.h>
int main()
{
int num=0;
printf("Enter Your number to check your even or odd: ");
// get input and store in num variable
scanf("%d",&num);
if(num%2==0)
{
printf("Given Number is even number.");
}
else
{
printf("Given Number is odd number.");
}
return 0;
}