C Program To Count Number of Even, Odd and Zeros In An Array

Lets write a C program to count number of even, odd and zeros in an array.

What are Even and Odd Numbers?

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.

Related Read:
Even or Odd Number: C Program

Example: Expected Output

Enter 10 integer numbers
10
15
0
-1
5
20
21
55
69
40

Even Numbers: 3
Odd Numbers: 6
Zeros: 1

Visual Representation

count even odd zero in an array

Video Tutorial: C Program To Count Number of Even, Odd and Zeros In An Array


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

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

Source Code: C Program To Count Number of Even, Odd and Zeros In An Array

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, odd = 0, even = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < N; i++)
    {
        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven Numbers: %d\nOdd Numbers: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

Output:
Enter 10 integer numbers
9
8
7
6
5
4
3
2
1
0

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

Logic To Find Number of Even, Odd and Zeros In An Array

First we ask the user to enter N integer numbers. After that we iterate through the array elements one by one (using a for loop) and check if the fetched number is 0, if true, we’ll increment the value of zero by one. If the fetched element is not zero, then we check if its perfectly divisible by 2, if so, then its even number orelse its odd number – and we increment the values of variable even and odd accordingly.

Note: Make sure to first check if the fetched number is 0. Only after checking this, go further and check for even or odd conditions.

Method 2

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, even = 0, odd = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven No: %d\nOdd No: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

Output:
Enter 10 integer numbers
0
1
2
3
4
5
6
7
8
9

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

In above source code, once the user inputs a number, we check if the input number is equal to zero or is perfectly divisible by 2 or it is not perfectly divisible by 2. Based on that we increment the values of variable zero, even and odd accordingly.

This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. So less overhead and more efficient.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

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

Size of Array using Macro and Constant: C Program

In this video tutorial lets see how we can assign size of an array using macros and constants.

Related Read:
For Loop In C Programming Language
Introduction To Arrays: C Programming Language
Keywords, Constants, Variables: C

Disadvantage of Not Using Macro or Constant To Assign Array Size

If requirement of the program/software changes and you need to increase or decrease the array size, then you’ll have to be very careful and scan through the entire source code and make changes at multiple locations. Even if you skip changing the array size information at one place, you’ll start getting wrong results.

And if you have any business logic which makes use of array size, then you’ll have hard time rectifying and debugging the code. It’ll take unnecessary time and effort to make it work correctly once again.

By making use of Macros or constant variables you can handle this very efficiently. You can make the change at one place and it’ll take effect at all the places in your source code.

Watch the video below for demonstration of effectiveness of using macros and constants for assigning size of an array.

Video Tutorial: Assign Size of Array using Macro and Constant: C Program


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

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

Source Code: Size of Array using Macro and Constant: C Program

Without using Macros and/or constants

#include<stdio.h>

int main()
{
    int a[5], i;

    printf("Enter 5 integer numbers\n");
    for(i = 0; i < 5; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = 0; i < 5; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
1
2
3
4
5

Here we have a array variable with size 5. We enter 5 integer variables and we display those elements using for loop. In for loop condition we mention the number of times it has to iterate.

Now assume that the requirement changes and we need to increase the size of array from 5 to 7:

#include<stdio.h>

int main()
{
    int a[7], i;

    printf("Enter 7 integer numbers\n");
    for(i = 0; i < 7; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = 0; i < 7; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 7 integer numbers
1
2
3
4
5
6
7
Array elements are:
1
2
3
4
5
6
7

As you can see we made edits at 4 places. Its a very simple program and even in that we had to make 4 edits. What if the program is huge and the requirement changes?

Macros

Assign Array size using Macros

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
1
2
3
4
5

Observe the changes we’ve made in the source code. We’re defining a macro here. Macro template is N and macro expansion is 5. We replace the value 5 inside main method by macro name N.

#include<stdio.h>

#define N 7

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 7 integer numbers
1
2
3
4
5
6
7
Array elements are:
1
2
3
4
5
6
7

Assume that the requirement changes and the client wants a list size of 7. Now instead of editing at multiple places, we only change the macro expansion from 5 to 7, and it starts working as intended.

Constants

Assign Array size using Constants

#include<stdio.h>

int main()
{
    const int N = 5;
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
1
2
3
4
5

Observe the changes we’ve made in the source code. We’ve declared and initialized a constant variable N. Constant variable name is N and its value is 5. We replace the value 5 inside main method by constant variable N.

#include<stdio.h>

int main()
{
    const int N = 7;
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 7 integer numbers
1
2
3
4
5
6
7
Array elements are:
1
2
3
4
5
6
7

Assume that the requirement changes and the client wants a list size of 7. Now instead of editing at multiple places, we only change the value of constant variable N from 5 to 7, and the program works as intended.

Note: It’s always considered best practice to either use macros or constant variables to assign array size. This practice will prove to be very advantageous while writing big programs.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

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

C Program To Determine Leap Year or Not using Macros

In this video tutorial lets learn how to determine user input year is a leap year or not, using Macros and nested ternary / conditional operator.

Related Read:
C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator
Using Macro Template In Macro Expansion: C Program

Leap Year Logic

1. If a year is century year(year ending with 00) and is perfectly divisible by 400, then it’s a leap year.

2. If a year is not a century year, and is perfectly divisible by 4, then it’s a leap year.

Video Tutorial: Determine Leap Year or Not using Macros: C Program


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

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

Source Code: C Program To Determine Leap Year or Not using Macros

#include<stdio.h>

#define NOT_LEAP(x) printf("%d is not leap year\n", x)
#define LEAP_YEAR(x) printf("%d is leap year\n", x)

#define LEAP(x) ( (x % 100 == 0 && x % 400 == 0) ? LEAP_YEAR(x) : \
                 ( (x % 4 ==0) ? LEAP_YEAR(x) : NOT_LEAP(x)) )

int main()
{
    int year;

    printf("Enter a year\n");
    scanf("%d", &year);

    LEAP(year);

    return 0;
}

Output 1:
Enter a year
2018
2018 is not leap year

Output 2:
Enter a year
2019
2019 is not leap year

Output 3:
Enter a year
2020
2020 is leap year

Output 4:
Enter a year
2023
2023 is not leap year

Output 5:
Enter a year
2024
2024 is leap year

In above program we’re writing NOT_LEAP(x) macro to display a message that the user input year is not a leap year. LEAP_YEAR(x) macro is used to display message that the user input year is a leap year.

We use both NOT_LEAP(x) and LEAP_YEAR(x) macro names or macro templates inside LEAP(x) macro expansion.

We’re also using macro continuation(\) to break the line and continue writing the logic in next line in macro expansion of LEAP(x).

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

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

Swap 2 Numbers Using Macros: C Program

Today lets learn how to swap two integer numbers(using a temporary variable) using Macros in C.

Video Tutorial: Swap 2 Numbers Using Macros: C Program


[youtube https://www.youtube.com/watch?v=esQ-DmyPYYc]

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

Source Code: Swap 2 Numbers Using Macros: C Program

#include<stdio.h>

#define SWAP(x, y, temp) temp = x; x = y; y = temp;

int main()
{
    int a, b, temp;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &a, &b);

    printf("Before swapping: a = %d and b = %d\n", a, b);

    SWAP(a, b, temp);

    printf("After swapping: a = %d and b = %d\n", a, b);

    return 0;
}

Output:
Enter 2 integer numbers
20
50
Before swapping: a = 20 and b = 50
After swapping: a = 50 and b = 20

Logic To Swap Two Numbers

First value of a is transferred to temp;
Next value of b is transferred to a.
Next value of temp is transferred to b.



That’s how value of a and b are swapped using a temporary variable.

Note: Preprocessor replaces the macro template(SWAP(a, b, temp)) with its corresponding macro expansion(temp = x; x = y; y = temp;) before passing the source code to the compiler.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

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

Macro Continuation (\) Preprocessor Operator: C Program

In this video lets see how we can have multiple line of code inside macro expansion, by using preprocessor operator – macro continuation( \ ).

Where Is It Used?

While you’re writing complex logic inside macro expansion, you’ll need to break the line and write code in next line. In such cases macro continuation operator is very helpful. And the code looks much cleaner and clearer.

Video Tutorial: Macro Continuation (\) Preprocessor Operator: C Program


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

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

Source Code: Macro Continuation (\): C Program

#include<stdio.h>

#define SQUR(x) \
        printf("Square of %d is %d\n", x, (x * x));

int main()
{
    SQUR(5);

    return 0;
}

Output:
Square of 5 is 25

Here we are writing the macro expansion in the next line, so we are making use of macro continuation preprocessor operator (\).

Source Code: Macro Continuation (\) Preprocessor Operator: C Program

#include<stdio.h>

#define COMPANY(x) switch(x) { \
                     case 1: printf("1. Oracle\n"); break; \
                     case 2: printf("2. IBM\n"); break; \
                     case 3: printf("3. Ripple\n"); break; \
                     default: printf("default. Banks\n"); \
                   }

int main()
{
    COMPANY(3);
    COMPANY(2);
    COMPANY(50);

    return 0;
}

Output:
3. Ripple
2. IBM
default. Banks

Here we’ve multiple lines of code inside macro expansion. So at the end of each line we’ve written macro continuation symbol ( \ – backslash). Wherever you write the macro template, preprocessor will replace it with the macro expansion before execution.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

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