Can anybody help me to solve this problem?
My program doesn't work as what it is required. Here is the question and my program code.
Thanks.

/**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. **/

#include<stdio.h>

int main()
{ 
int num[5];
int i;

printf("Enter 5 int numbers ");
for (i=0;i<=4;i++){
scanf("%d",&num[i]);
}
for(i=0;i<=4;i++){	
	if(num[i]%2==0){
	printf("\nEven number : %d",num[i]);
	}
	else if(num[i]<0){
	printf("\nNegative number : %d",num[i]);
	}
	else if(num[i]%2!=0){
	printf("\nOdd number : %d",num[i]);
	}
	else if(num[i]>=0){
	printf("\nPositive number : %d",num[i]);
	}
}

return 0;
}

Can anybody help me to solve this problem?
My program doesn't work as what it is required. Here is the question and my program code.
Thanks.

/**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. **/

#include<stdio.h>

int main()
{ 
int num[5];
int i, even_number, odd_number, positive_number, negative_number;


printf("Enter 5 int numbers ");
for (i=0;i<=4;i++){
scanf("%d",&num[i]);
}
even_number = odd_number = positive_number = negative_number = 0;
for(i=0;i<=4;i++){	
	if(num[i]%2==0){
	//printf("\nEven number : %d",num[i]);
        even_number++;
        }
	else if(num[i]<0){
	//printf("\nNegative number : %d",num[i]);
        negative_number++;
	}
        //like the above, for the last two types of numbers.
	else if(num[i]%2!=0){
	printf("\nOdd number : %d",num[i]);
	}
	else if(num[i]>=0){
	printf("\nPositive number : %d",num[i]);
	}
}

printf("\n There were %d even numbers, %d odd numbers, %d negative numbers, and %d positive numbers", 
even_numbers, odd_numbers, negative_numbers, positive_numbers);

return 0;
}

That should get you off to a good start.

Hey Wait!!

U want to find out how many of them are positive, how many are negative, how many are even and how many odd. But your code will give wrong result. For example u entered 5 as one of the five numbers, then ur code will consider it as odd and but it is also a positive number. But because u have used nested if-else it will consider it odd only(not positive). To avoid this change ur code like this:

/**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. **/

#include<stdio.h>

int main()
{ 
int num[5];
int i, even_number, odd_number, positive_number, negative_number;


printf("Enter 5 int numbers ");
for (i=0;i<=4;i++){
scanf("%d",&num[i]);
}
even_number = odd_number = positive_number = negative_number = 0;
for(i=0;i<=4;i++){	
	if(num[i]%2==0){
	//printf("\nEven number : %d",num[i]);
        even_number++;
        }
        if(num[i]<0){
	//printf("\nNegative number : %d",num[i]);
        negative_number++;
	}
        //like the above, for the last two types of numbers.
       if(num[i]%2!=0){
	printf("\nOdd number : %d",num[i]);
	}
       if(num[i]>=0){
	printf("\nPositive number : %d",num[i]);
	}
}

printf("\n There were %d even numbers, %d odd numbers, %d negative numbers, and %d positive numbers", 
even_numbers, odd_numbers, negative_numbers, positive_numbers);

return 0;
}

Now it will pass both conditions for odd and positive.

This article has been dead for over six months. Start a new discussion instead.