How to write a C Program to Put Even and Odd Numbers in two Separate Arrays using For Loop, While Loop, Functions with example.
C Program to Put Even and Odd Numbers in two Separate Arrays
This program allows the user to enter the Size and the row elements of One Dimensional Array.
/* C Program to Put Even and Odd Numbers in two Separate Arrays */ #include<stdio.h> void PrintArray(int a[], int Size); int main() { int Size, i, a[10], Even[10], Odd[10]; int Even_Count = 0, Odd_Count = 0; printf("\n Please Enter the Size of an Array : "); scanf("%d", &Size); printf("\n Please Enter the Array Elements : "); for(i = 0; i < Size; i++) { scanf("%d", &a[i]); } for(i = 0; i < Size; i ++) { if(a[i] % 2 == 0) { Even[Even_Count] = a[i]; Even_Count++; } else { Odd[Odd_Count] = a[i]; Odd_Count++; } } printf("\n Total Number of Even Numbers in this Array = %d ", Even_Count); printf("\n Array Elements in Even Array : "); PrintArray(Even, Even_Count); printf("\n Total Number of Odd Numbers in this Array = %d ", Odd_Count); printf("\n Array Elements in Odd Array : "); PrintArray(Odd, Odd_Count); return 0; } void PrintArray(int a[], int Size) { int i; for(i = 0; i < Size; i++) { printf("%d \t ", a[i]); } printf("\n"); }
Here, For Loop will make sure that the number is between 0 and maximum size value. In this C Programming example, it will be from 0 to 4
for(i = 0; i < Size; i ++)
In the Next line, We declared the If statement
if(a[i] % 2 == 0) { Even[Even_Count] = a[i]; Even_Count++; }
Any number that is divisible by 2 is even number. If condition checks whether the remainder of the current One Dimensional Array element divided by 2 is exactly equal to 0 or not.
- If the condition is True, then it is an Even number. So compiler will assign that element to Even Array at first position, and next, it will increment the Even_Count value.
- If the condition is False, then it is an Odd number. So the compiler will assign that element to Odd Array at first position, and next, it will increment Odd_Count value.
void PrintArray(int a[], int Size) function is to print array elements in Even array, and Odd array
C Program to Put Even and Odd Numbers in two Separate Arrays using While Loop
This program is the same as above. But this time we used While Loop to Put Even and Odd Numbers in two Separate Arrays
/* C Program to Put Even and Odd Numbers in two Separate Arrays */ #include<stdio.h> void PrintArray(int a[], int Size); int main() { int Size, i, j = 0, a[10], Even[10], Odd[10]; int Even_Count = 0, Odd_Count = 0; printf("\n Please Enter the Size : "); scanf("%d", &Size); printf("\n Please Enter the Elements : "); for(i = 0; i < Size; i++) { scanf("%d", &a[i]); } while(j < Size) { if(a[j] % 2 == 0) { Even[Even_Count] = a[j]; Even_Count++; } else { Odd[Odd_Count] = a[j]; Odd_Count++; } j++; } printf("\n Total Number of Even Numbers = %d ", Even_Count); printf("\n Elements in Even Array : "); PrintArray(Even, Even_Count); printf("\n Total Number of Odd Numbers = %d ", Odd_Count); printf("\n Elements in Odd Array : "); PrintArray(Odd, Odd_Count); return 0; } void PrintArray(int a[], int Size) { int i = 0; while(i < Size) { printf("%d \t ", a[i]); i++; } printf("\n"); }
Please Enter the Size : 7
Please Enter the Elements : 10 25 69 89 76 23 198
Total Number of Even Numbers = 3
Elements in Even Array : 10 76 198
Total Number of Odd Numbers = 4
Elements in Odd Array : 25 69 89 23
Program to Put Even and Odd Numbers in two Separate Arrays using Functions
This program is the same as the first example. However, we separated the logic to put even numbers and odd numbers in separate arrays using Functions.
/* C Program to Put Even and Odd Numbers in two Separate Arrays */ #include<stdio.h> void CountEvenNumbers(int a[], int Size); void CountOddNumbers(int a[], int Size); void PrintArray(int a[], int Size); int main() { int Size, i, a[10]; printf("\n Please Enter the Size : "); scanf("%d", &Size); printf("\n Please Enter the Elements : "); for(i = 0; i < Size; i++) { scanf("%d", &a[i]); } CountEvenNumbers(a, Size); CountOddNumbers(a, Size); return 0; } void CountEvenNumbers(int a[], int Size) { int i, Even[10], Even_Count = 0; printf("\n Elements in Even Array: "); for(i = 0; i < Size; i ++) { if(a[i] % 2 == 0) { Even[Even_Count] = a[i]; Even_Count++; } } PrintArray(Even, Even_Count); printf(" Total Number of Even Numbers = %d ", Even_Count); } void CountOddNumbers(int a[], int Size) { int i, Odd[10], Odd_Count = 0; printf("\n\n Elements in Odd Array : "); for(i = 0; i < Size; i ++) { if(a[i] % 2 != 0) { Odd[Odd_Count] = a[i]; Odd_Count++; } } PrintArray(Odd, Odd_Count); printf(" Total Number of Odd Numbers = %d ", Odd_Count); } void PrintArray(int a[], int Size) { int i; for(i = 0; i < Size; i++) { printf("%d \t ", a[i]); } printf("\n"); }
Please Enter the Size : 10
Please Enter the Elements : 12 35 69 75 105 22 98 76 22 53
Elements in Even Array: 12 22 98 76 22
Total Number of Even Numbers = 5
Elements in Odd Array : 35 69 75 105 53
Total Number of Odd Numbers = 5