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 Odd Numbers From 1 To N, using For loop

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

Odd Number: An odd number is an integer that is not exactly divisible by 2.

For Example: 7 % 2 != 0. When we divide 7 by 2, it doesn’t give a reminder of 0. So number 7 is odd number.

Related Read:
Decision Control Instruction In C: IF
For Loop In C Programming Language
Even or Odd Number: C Program

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

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


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

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

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

Since we are checking for odd 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 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 odd numbers from 1 to user entered number.

Source Code: C Program To Find Sum of All Odd 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("Odd 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 odd numbers from 1 To %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter the limit
5
Odd numbers from 1 To 5 are:
1
3
5
Sum of odd numbers from 1 To 5 is 9

Output 2:
Enter the limit
10
Odd numbers from 1 To 10 are:
1
3
5
7
9
Sum of odd numbers from 1 To 10 is 25

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

Lets write a C program to generate odd numbers between 2 integer values input by the user using For loop.

Related Read:
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers

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

Note 2: Odd numbers are of the form (2 * number + 1);

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

Video Tutorial: C Program To Find Odd Numbers Between Range using For Loop


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

YouTube Link: https://www.youtube.com/watch?v=P85sziIyIdA [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. If value of start is greater than the value of end, then we swap the values.

For loop counter is initialized to start, and for loop executes until value of count is less than or equal to end. For each iteration of the for loop, count value increments by 1.

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

Source Code: C Program To Find Odd Numbers Between Range using For Loop

 
#include<stdio.h>

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

    printf("Enter start and end value, to find odd numbers\n");
    scanf("%d%d", &start, &end);

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

    printf("Odd 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 and end value, to find odd numbers
40
60
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59

Output 2
Enter start and end value, to find odd numbers
60
40
Odd numbers between 40 and 60 are
41
43
45
47
49
51
53
55
57
59

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 Odd Numbers From 1 To N, using While loop

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

Odd Number: An odd number is an integer that is not exactly divisible by 2.

For Example: 3 % 2 != 0. When we divide 3 by 2, it doesn’t give a reminder of 0. So number 3 is odd number.

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program

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


[youtube https://www.youtube.com/watch?v=y4Sy9xo-pFU]

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

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

Since we are checking for odd 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 odd numbers from 1 to user entered number.

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

#include<stdio.h>

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

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

    while(count <= num)
    {
        if(count%2 != 0)
        {
            sum = sum + count;
        }
        count++;
    }

    printf("Sum of ODD integer number is %d\n", sum);

    return 0;
}

Output 1:
Enter a integer number
5
Sum of ODD integer number is 9

Output 2:
Enter a integer number
20
Sum of ODD integer number is 100

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

C program to generate odd 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 odd number is an integer that is not exactly divisible by 2.

Note 2: Odd numbers are of the form (2 * n + 1);

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

C Program to Generate Odd Numbers Between Two Integers

 
#include<stdio.h>

int main()
{
    int count, limit;

    printf("Enter start and end value to generate Odd numbers\n");
    scanf("%d%d", &count, &limit);

    printf("\nOdd 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 and end value to generate Odd numbers
10
30

Odd numbers between 10 and 30 are:
11
13
15
17
19
21
23
25
27
29

Output 2
Enter start and end value to generate Odd numbers
1
10

Odd numbers between 1 and 10 are:
1
3
5
7
9

C Program to Generate Odd Numbers Between Two Integers


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

YouTube Link: https://www.youtube.com/watch?v=vPzV7pE0Djw [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 not perfectly divisible by 2. If true, it’s a Odd 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