C Programming Code Examples

C > Arrays and Matrices Code Examples

program to count even and odd elements in an array

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/* program to count even and odd elements in an array Write a C program to read elements in an array from user and count total number of even and odd elements in the given array. How to find total number of even and odd elements in a given array using C programming. Logic to count total even or odd elements in array Input elements in array. Run a loop from 0 to N-1 (Where N is the total number of elements in array). Inside loop current array element is even then, increment the even count by 1. Otherwise increment the odd count. Print the values of even and odd count after the termination of loop. */ #include <stdio.h> #define maxsize 100 //Maximum size of the array int main() { int arr[maxsize]; int j, N, even, odd; /* Input size of the array */ printf("Enter size of the array: "); scanf("%d", &N); /* Input array elements */ printf("Enter %d elements in array: ", N); for(j=0; j<N; j++) { scanf("%d", &arr[j]); } /* Assuming that there are 0 even and odd elements */ even = 0; odd = 0; for(j=0; j<N; j++) { /* If the current element of array is even then increment even count */ if(arr[j]%2 == 0) { even++; } else { odd++; } } printf("Total even elements: %d\n", even); printf("Total odd elements: %d", odd); return 0; }

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

This, is, a, test to analyze comma-separated list. Skip over spaces and commas. Copy characters that are not space, comma or \0 as part of a word. Append terminator and The

C Program checks whether the Singly Linked list is a palindrome. A Palindrome is a Pattern in List in which the contents when read from front is the same as when read from last. So

C Program to performs Comb sort on array of integers. "Comb Sort" is a comparison sorting algorithm. This sort is 'Exchange sort', similar to 'Bubble Sort'. Function to find the new gap

This program takes the 3 numbers & finds the biggest among all. Take the three numbers as input and Check the First number if it Greater than other two. Repeat the step 2 for other 2

Returns a pointer to the first member that matches *key. A null pointer is returned if not found. The array must be sorted in ascending order. The number of elements in the array is