C Program To Count Positive, Negative, Even And Odd Numbers In An Array

Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd.

Important Note
Most of the time 0 is considered as even number, as it has odd numbers on either side of it i.e., 1 and -1. And 0 is perfectly divisible by 2.

Also note that 0 is neither positive and nor negative.

number scale

Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array
C Program To Count Number of Positive, Negative and Zeros In An Array

Example: Expected Output

Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1

Visual Representation

count positive negative even and odd elements in array

Video Tutorial: C Program To Count Positive, Negative, Even And Odd Numbers In An Array


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

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

Source Code: C Program To Count Positive, Negative, Even, Odd And Zero Numbers In An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, pos = 0, neg = 0, 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] > 0)
            pos++;
        else
            neg++;

        if(a[i] == 0)
        {

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

    printf("\nPositive: %d\n", pos);
    printf("Negative: %d\n", neg);
    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);
    printf("Zero: %d\n", zero);

    return 0;
}

Output:
Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1

Here we are counting positive numbers, negative numbers, even numbers, odd numbers and zeros.

Source Code: C Program To Count Positive, Negative, Even And Odd Numbers In An Array

#include<stdio.h>

#define N 10

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

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

        }
        else if(a[i] > 0)
            pos++;
        else
            neg++;

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

    printf("\nPositive: %d\n", pos);
    printf("Negative: %d\n", neg);
    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);

    return 0;
}

Output:
Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5

Positive: 5
Negative: 4
Even: 5
Odd: 5

In above source code, since 0 is perfectly divisible by 2, it is considered as even number.

Logic To Count Positive, Negative, Even And Odd Numbers In An Array

We ask the user to enter N integer numbers and store it inside array variable a[N]. Next we loop through the array using for loop. For each iteration we check: if the selected number is greater than 0. If true, then its positive number, so we increment the value of pos by 1. If the number is less than 0, then its a negative number, so we increment the value of neg by 1.

Next we check if the selected number is perfectly divisible by 2. If true, then its even number, so we increment the value of variable even by 1. If the selected number is not perfectly divisible by 2, then its odd number.

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 Find Sum of All Even Numbers From 1 To N, using For loop

Lets write a C program to find sum of all the even numbers from 1 to N, using for loop.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 8 % 2 == 0. When we divide 8 by 2, it give a reminder of 0. So number 8 is an even number.

If user enters num = 5. Even numbers between 1 to 5 are 2, 4. So we add 2 and 4. i.e., 2 + 4 = 6. We display 6 to the console window as result.

Related Read:
Decision Control Instruction In C: IF
For Loop In C Programming Language
Even or Odd Number: C Program
C Program To Find Even Numbers Between Range using For Loop

You can also watch the video for C Program To Find Sum of All Even Numbers From 1 To N, using While loop

Video Tutorial: C Program To Find Sum of All Even Numbers From 1 To N, using For loop


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

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

Logic To Find Sum of All Even Numbers From 1 To N, using For loop

Since we are checking for even numbers from 1 to user entered number, we assign value of variable count to 1. For loop keeps iterating until value of count is less than or equal to value of user input number. For each iteration of for loop we increment the value of count by 1.

Inside for loop we check for the condition, count%2 == 0. If it’s true, then we add the value present inside variable count to previous value present in variable sum.

After the control exits for loop we display the value present in variable sum – which has the sum of all the even numbers from 1 to user entered number.

Source Code: C Program To Find Sum of All Even Numbers From 1 To N, using For loop

#include<stdio.h>

int main()
{
    int count, num, sum = 0;

    printf("Enter the limit\n");
    scanf("%d", &num);

    printf("Even numbers from 1 To %d are:\n", num);
    for(count = 1; count <= num; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
            sum = sum + count;
        }

    }

    printf("Sum of even numbers from 1 To %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter the limit
5
Even numbers from 1 To 5 are:
2
4
Sum of even numbers from 1 To 5 is 6

Output 2:
Enter the limit
10
Even numbers from 1 To 10 are:
2
4
6
8
10
Sum of even numbers from 1 To 10 is 30

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 Find Even Numbers Between Range using For Loop

Lets write a C program to find all the even numbers between 2 integer values input by the user, using for loop.

Related Read:
Even or Odd Number: C Program
Modulus or Modulo Division In C Programming Language
C Program to Generate Even Numbers Between Two Integers

Note 1: An even number is an integer that is exactly divisible by 2. That is reminder of division should be zero.

Note 2: Even numbers are of the form 2 * number;

Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.

Video Tutorial: C Program to Find Even Numbers Between Range using For Loop


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

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


In above c program, we ask the user to input 2 integer value and store it in variables start and end. For loop counter is initialized to start and for loop executes until loop counter is less than or equal to value of end. For each iteration of the for loop, loop counter value increments by 1.

Inside for loop, for every value of count, we check if its perfectly divisible by 2. If true, it’s a Even number and we output that number to the console window.

Source Code: C Program to Find Even Numbers Between Range using For Loop

 
#include<stdio.h>

int main()
{
    int start, end, count, temp;

    printf("Enter start value and end value to generate Even no's\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp  = start;
        start = end;
        end   = temp;
    }

    printf("Even numbers between %d and %d are:\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
        }
    }

    return 0;
}

Output 1
Enter start value and end value to generate Even no’s
10
20
Even numbers between 10 and 20 are:
10
12
14
16
18
20

Output 2
Enter start value and end value to generate Even no’s
50
40
Even numbers between 40 and 50 are:
40
42
44
46
48
50

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 Find Sum of All Even Numbers From 1 To N, using While loop

Lets write a C program to find sum of all the even numbers from 1 to N, using while loop.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 8 % 2 == 0. When we divide 8 by 2, it give a reminder of 0. So number 8 is an even number.

If user enters num = 5. Even numbers between 1 to 5 are 2, 4. So we add 2 and 4. i.e., 2 + 4 = 6. We display 6 to the console window as result.

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program
C Program to Generate Even Numbers Between Two Integers

Video Tutorial: C Program To Find Sum of All Even Numbers From 1 To N, using While loop


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

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

Logic To Find Sum of All Even Numbers From 1 To N, using While loop

Since we are checking for even numbers from 1 to user entered number, we assign value of variable count to 1. While loop keeps iterating until value of count is less than or equal to value of user input number. For each iteration of while loop we increment the value of count by 1.

Inside while loop we check for the condition, count%2 == 0. If it’s true, then we add the value present inside variable count to previous value present in variable sum.

After the control exits while loop we display the value present in variable sum – which has the sum of all the even numbers from 1 to user entered number.

Source Code: C Program To Find Sum of All Even Numbers From 1 To N, using While loop

#include<stdio.h>

int main()
{
    int num, count = 1, sum = 0;

    printf("Enter the limit\n");
    scanf("%d", &num);

    while(count <= num)
    {
        if(count%2 == 0)
        {
            sum = sum + count;
        }
        count++;
    }
    printf("Sum of Even numbers from 1 to %d is %d\n", num, sum);
    return 0;
}

Output 1:
Enter the limit
5
Sum of Even numbers from 1 to 5 is 6

Output 2:
Enter the limit
20
Sum of Even numbers from 1 to 20 is 110

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 Generate Even Numbers Between Two Integers

C program to generate even numbers between 2 integer values input by the user.

Related Read:
Even or Odd Number: C Program
Even or Odd Number without using Modular Division: C Program

Note 1: An even number is an integer that is exactly divisible by 2.

Note 2: Even numbers are of the form 2 * n;

Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.

C Program to Generate Even Numbers Between Two Integers

 
#include<stdio.h>

int main()
{
    int count, limit;

    printf("Enter start value and end value to generate Even no's\n");
    scanf("%d%d", &count, &limit);

    printf("\nEven numbers between %d and %d are:\n", count, limit);

    while(count <= limit)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
        }
        count++;
    }

    return 0;
}

Output 1
Enter start value and end value to generate Even no’s
10
30

Even numbers between 10 and 30 are:
10
12
14
16
18
20
22
24
26
28
30

Output 2
Enter start value and end value to generate Even no’s
1
10

Even numbers between 1 and 10 are:
2
4
6
8
10

C Program to Generate Even Numbers Between Two Integers


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

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


In above c program, we ask the user to input 2 integer value and store it in variables count and limit. While loop keeps executing until the start value i.e., count is less than or equal to the end value i.e., limit.

Inside while loop, for every value of count, we check if its perfectly divisible by 2. If true, it’s a Even number and we output that number to the console window. On every iteration of the loop we increment the value of count by 1.

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

Update:
In the above video I’ve said: “count should be exactly divisible by 0”.

I’m sorry about that. It should be – “Value inside variable count should be exactly divisible by 2.”