Even or Odd Number: C Program

Today lets write a C program to check whether a user entered integer number is EVEN or ODD.

Related Read:
Basic Arithmetic Operations In C
Relational Operators In C

Even or Odd Number without using Modular Division: C Program
Even or Odd Number using Ternary Operator: C Program

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

C program To check Even or Odd Number: Source Code

 
#include < stdio.h >

int main()
{
    int n;

    printf("Enter an integer number\n");
    scanf("%d", &n);

    if(n % 2 == 0)
    {
        printf("%d is even number\n", n);
    }
    else
    {
        printf("%d is odd number\n", n);
    }

    return 0;
}

Output 1
Enter an integer number
2
2 is even number

Output 2
Enter an integer number
3
3 is odd number

Output 3
Enter an integer number
7
7 is odd number

Output 4
Enter an integer number
12
12 is even number

C program To check Even or Odd Number


[youtube https://www.youtube.com/watch?v=CAgJxUh8bEg]

YouTube Link: https://www.youtube.com/watch?v=CAgJxUh8bEg [Watch the Video In Full Screen.]


Modular division returns remainder of division. For example, 12 / 2 = 6. But 12 % 2 = 0.

In above c program, we ask the user to input an integer value and store it in variable n. Next using if else condition, we check if the user entered number is perfectly divisible by 2. If its perfectly divisible by 2, then it’e even number or else its odd number.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Basic Arithmetic Operations In C

Today lets learn about basic arithmetic operations in C programming language.

What we learn in this video tutorial?

We shall learn, addition, subtraction, multiplication, division and modular division in this video tutorial. And we’ll also learn how to use pow() method present in math.h library file.

 
#include < stdio.h >
#include < math.h >

int main()
{
    int   a = 10, b = 2;
    char  m = 'A', n = 'a';

    printf("\nAddition of a and b is %d\n", (a+b));
    printf("\nSubtraction of a and b is %d\n", (a-b));
    printf("\nMultiplication of a and b is %d\n", (a*b));
    printf("\nDivision of a and b is %d\n", (a/b));
    printf("\nModular Division of a and b is %d\n", (a%b));
    printf("\n3 to the power of 2 is %f\n", pow(3,2));

    printf("\nASIIC value of A and a is %d and %d\n\n", m, n);

    return 0;
}

Output:

Addition of a and b is 12
Subtraction of a and b is 8
Multiplication of a and b is 20
Division of a and b is 5
Modular Division of a and b is 0
3 to the power of 2 is 9.000000
ASIIC value of A and a is 65 and 97

Basic Arithmetic Operations In C


[youtube https://www.youtube.com/watch?v=4mjiL9uwPCg]

YouTube Link: https://www.youtube.com/watch?v=4mjiL9uwPCg [Watch the Video In Full Screen.]


Precedence and associativity of arithmetic operators

1st priority: * / %
2nd priority: + –
3rd priority: =

When there is tie between same priority operators then we check the associativity. For example, for * and /, associativity is same. i.e., left to right.

So in the expression: c = a * b + a / b;
a * b gets executed first.

ASCII values

When you try to apply Arithmetic Operation on characters(alphabets, character digits, special characters) its correspondent ASCII value gets operated on. ASCII value of A is 65 and ASCII value of a is 97. This is the reason why capital A and small letter a are treated differently in C programming language. Under the hood, they have different ASCII values.

Note 1: All the variables and constants are called operands. All the arithmetic symbols(+, -, /, *, %) are called operators. = is called assignment operator.

Note 2: Only one variable is allowed on the left hand side of the equation or expression.
Ex:
c = a + b is valid.
a + b = c is invalid.

Note 3: You can’t store a decimal value in a integer variable. So if the result of evaluation of an expression has decimal value and you’re assigning it to an integer variable, then only the integer part gets stored. Decimal part will be discarded. So be careful while choosing the data type of variables while performing arithmetic operation.

Note 4: We need to always, explicitly mention the arithematic operators.
Ex:
c = (a + b)(a – b) throws error.
c = (a + b) * (a – b); is the correct way in C programming.

Note 5: An integer value divided by an integer value gives back an integer value. A float value divided by a float value gives back a floating point value.

Note 6: Modular division gives reminder of division.
Ex:
c = 10 / 2; gives c = 5;
c = 10 % 2; give c = 0;

Also,
c = -3 % 2; gives c = -1
c = 3 % -2; gives c = 1

Modular division doesn’t take float values as its operands.
Ex: c = 3.0 % 2.0; gives error

Note 7: math.h library file has other mathematical operations related methods like: pow(), sin(), cos(), tan(), abs(), sqrt() etc.

We’ll look more arithmetic operators as and when we encounter them in programs.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert