Print odd or even numbers using One Dimensional Array in C


This program is a C program that takes an input of a limit and an array of values. It then uses a for loop to iterate through the array and check whether each value is even or odd. If the value is even, it prints the message "Even value" along with the index and value, and increments the variable "c" which keeps track of the number of even numbers. If the value is odd, it prints the message "Odd value" along with the index and value, and increments the variable "b" which keeps track of the number of odd numbers. Finally, it prints the total number of even numbers and the total number of odd numbers.

Source Code

#include<stdio.h>
int main()
{
  int i,j,a[10],n,m,b=0,c=0;
  printf("\nEnter the limit:");
  scanf("%d",&n);
  printf("\nEnter the values:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("\nGiven values are:");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  for(i=0;i<n;i++)
  {
    if(a[i]%2==0)
    {
      printf("\nEven value:\na[%d]=%d",i,a[i]);
      c++;
    }
    else
    {
      printf("\nOdd value:\na[%d]=%d",i,a[i]);
      b++;
    }
  }
  printf("\nNo of Even numbers:%d",c);
  printf("\nNo of Odd numbers:%d",b);
  return 0;
}
            
To download raw file Click Here

Output

Enter the limit:5
Enter the values:1
2
3
4
5
Given values are:
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Odd value:
a[0]=1
Even Value:
a[1]=2
Odd value:
a[2]=3
Even Value:
a[3]=4
Odd value:
a[4]=5
No. of Even numbers:2
No. of Odd numbers:3

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C