Nakagawa Yuuki Nakagawa Yuuki - 2 months ago 9
C Question

How can i know even and odd numbers from input by user in array? -c

I'm working on a program to count how many even numbers or odd numbers using Array, The User Input amount of array (if they input 4, there will be 4 looping of input numbers value) and i don't know how to count up even numbers or odd numbers from an Array..

#include<stdio.h> int main(){
int array[10];
int i,j,k;

printf("Amount of Array :");
scanf("%d", &j);

for(i=0;i<j;i++){
printf(" %d value : ",i+1);
scanf("%d", &array[i]);
}

printf("Amount of even numbers : %d", array[i]%2 );}


The finished program need to be like:


Amount Of Array: 5 (*)

1st value : 6 (*)

2nd value : 7 (*)

3rd value : 2 (*)

4th value : 3 (*)

5th value : 6 (*)

Amount of even numbers :3

The Even numbers are: 6 , 2 ,6

Amount of odd numbers :2

The odd numbers are: 7 , 3


*User input

Something like that, don't mind the ordinal indicator (-st,-nd,-rd) it's not written in english in the real program, sorry if there's some mispelled..

Answer

I would do something along these lines:

#include<stdio.h>

int main(){
  int array[10];
  int i,j,k;

  int even_number_count;
  int odd_number_count;

  printf("Amount of Array :");
  scanf("%d", &j);

  for(i=0;i<j;i++){
    printf(" %d value : ",i+1);
    scanf("%d", &array[i]);
  }

  even_number_count=0;
  odd_number_count=0;

  printf("Even numbers are: ");
  for(i=0;i<j;i++){
    if (array[i] % 2 == 0) {
      printf("%d  ", array[i]);
      even_number_count++;
    }
  }
  printf("\n");

  printf("Odd numbers are: ");
  for(i=0;i<j;i++){
    if (array[i] % 2 != 0) {
      printf("%d  ", array[i]);
      odd_number_count++;
    }
  }
  printf("\n");

  printf("Amount of even numbers : %d\n", even_number_count );
  printf("Amount of odd numbers : %d\n", odd_number_count );

  return 0;
}