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.
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
Page Contents
Enter 10 integer numbers
-4
-3
-2
-1
0
1
2
3
4
5
Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1
#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.
#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.
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