 # C Programming Code Examples

## C > Arrays and Matrices Code Examples

### sort array in ascending or descending order

``` sort array in ascending or descending order Write a C program to read elements in an array and sort elements of the array in ascending order. Read unsorted integer values in array and sort them in ascending order. There are numerous logic to sort a given set of numbers. Here I am using general algorithm which we apply in real life for simplicity. To sort array we select an element and place it to its correct position by comparing with subsequent elements. Input size of array and elements in array. Store it in some variable say size and array[]. To select each element from the array, run an outer loop from 0 to size - 1. The loop structure must look like for(x=0; x<size; x++). Run another inner loop from x + 1 to size - 1 to place the currently selected element at its correct position. The loop structure should look like for(j=x+1; j<size; j++). Inside the inner loop to compare the currently selected element with subsequent elements and perform swapping if needed. Which is if(array[x] > array[j]) then swap array[x] and array[j]. #include <stdio.h> #define maxsize 100 int main() { int array[maxsize]; int size; int x, j, temp; /* Input size of array */ printf("Enter size of array: "); scanf("%d", &size); /* Input elements in array */ printf("Enter elements in array: "); for(x=0; x<size; x++) { scanf("%d", &array[x]); } for(x=0; x<size; x++) { /* Place the currently selected element array[x] to its correct place. */ for(j=x+1; j<size; j++) { /* Swap if currently selected array element is not at its correct position. */ if(array[x] > array[j]) { temp = array[x]; array[x] = array[j]; array[j] = temp; } } } /* Print the sorted array */ printf("\nElements of array in sorted ascending order: "); for(x=0; x<size; x++) { printf("%d\t", array[x]); } return 0; } ```

C Program Code to Catch pressing Ctrl+C - Program example catches the Ctrl-C (SIGINT) signal for the first time and prints a output rather but exits on pressing ctrl-c again. You have presses Ctrl-C, please press again to exit

C Language Program Calculate Percentage - Percent means per cent (hundreds), a ratio of the parts out of 100. The symbol of percent is %. We count 'Percentage of Marks Obtained', return on investment and Percentage can go

Compare Both the String n Chars strncmp - C programming String functions "strncmp". It compare both the string till n characters or in other words it 'compares' first n characters of both the strings. Print string1 and string2 are

C Programming code Array Concatenation - To concate two arrays, we need at least three array variables. We shall take two arrays and then based on some constraint, will copy their content into one single array. We shall

C Code Check Palindrome Using Recursion - In this article we have shared two C programs to check if the input number is palindrome or not. 1) using while loop 2) using recursion. If a number 'remains same', even if we reverse its

C++ Language Character & String Literals - When writing both single "character & string" literals, it is Necessary to 'Put the Quotation' marks surrounding them to distinguish them from possible variable identifiers or reserved

Find The Perfect Number In C++ language - For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is 1 + 2+ 3 =6 and 28 is also a 'Perfect Number' since 1+ 2 + 4 + 7 + 14= 28. Other 'Perfect Numbers': 496

Switch Case Statement in C Programming - The Switch Case statement is used when we have multiple options & we need to perform a different task for each option. Case doesnt always need to have order 1, 2, 3 and so on...