➤ Program to print the reverse of an Array
- #include<stdio.h>
-
- int main()
- {
- printf("\n\n\t\tFRIDAY - Best place to learn\n\n\n");
-
- int c, d, n, a[100], b[100];
- printf("\n\nEnter number of elements in array : ");
- scanf("%d", &n);
- printf("\n\nEnter %d elements\n", n);
-
- for(c = 0; c < n; c++)
- scanf("%d", &a[c]);
-
- /*
- temporarily storing elements into array b
- starting from end of array a
- */
- for(c = n-1, d = 0; c >= 0; c--, d++)
- b[d] = a[c];
-
- /*
- copying reversed array into original.
- Here we are modifying original array to reverse it.
- */
-
- for(c = 0; c < n; c++)
- a[c] = b[c];
-
- printf("\n\n Resultant array is: ");
- for(c = 0; c < n; c++)
- printf("%d", a[c]);
-
- return 0;
- }
-
- OUTPUT :
-
-
- FRIDAY - Best place to learn
-
-
-
-
- Enter number of elements in array : 5
-
-
- Enter 5 elements
- 3
- 4
- 2
- 4
- 5
-
-
- Resultant array is: 54243
➤ Program to insert an element in an Array .
- #include<stdio.h>
-
- int main()
- {
- int array[100], position, c, n, value;
-
- printf("\n\nEnter number of elements in array: ");
- scanf("%d", &n);
-
- printf("\n\nEnter %d elements\n", n);
- for(c = 0; c < n; c++)
- scanf("%d", &array[c]);
-
- printf("\n\nEnter the location where you want to insert new element: ");
- scanf("%d", &position);
-
- printf("\n\nEnter the value to insert: ");
- scanf("%d", &value);
-
- // shifting the elements from (position to n) to right
- for(c = n-1; c >= position-1; c--)
- array[c+1] = array[c];
-
- array[position - 1] = value; // inserting the given value
-
- printf("\n\nResultant array is: ");
- /*
- the array size gets increased by 1
- after insertion of the element
- */
- for(c = 0; c <= n; c++)
- printf("%d ", array[c]);
-
- return 0;
- }
-
-
- OUTPUT :
-
- Enter number of elements in array: 5
- Enter 5 elements
- 2
- 3
- 4
- 5
- 6
- Enter the location where you want to insert new element: 1
- Enter the value to insert: 8
- Resultant array is: 8 2 3 4 5 6
-
- Again execute code :
- Enter number of elements in array: 5
- Enter 5 elements
- 1
- 2
- 3
- 4
- 5
- Enter the location where you want to insert new element: 6
- Enter the value to insert: 6
- Resultant array is: 1 2 3 4 5 6
➤ Program to Delete an element in an Array in C.
- #include<stdio.h>
-
- int main()
- {
-
-
- int array[100], position, c, n;
- printf("\n\nEnter number of elements in array: ");
- scanf("%d", &n);
-
- printf("\n\nEnter %d elements\n", n);
- for(c = 0; c < n; c++)
- scanf("%d", &array[c]);
-
- printf("\n\nEnter the location where you want to delete element from: ");
- scanf("%d", &position);
-
- if(position >= n+1)
- printf("\n\nDeletion not possible\n\n");
- else
- // updating the locations with next elements
- for(c = position-1; c < n-1; c++)
- array[c] = array[c+1];
-
- printf("\n\nResultant array is: ");
- /*
- the array size gets reduced by 1
- after deletion of the element
- */
- for(c = 0; c < n-1; c++)
- printf("%d ", array[c]);
-
- return 0;
- }
-
- OUTPUT :
-
-
- Enter number of elements in array: 5
- Enter 5 elements
- 11
- 12
- 13
- 14
- 15
- Enter the location where you want to delete element from: 2
- Resultant array is: 11 13 14 15
➤ Program to Sum of N element in an Array in C.
- #include<stdio.h>
-
- int main()
- {
-
- int n, sum = 0, c, array[100];
-
- printf("Enter the number of integers you want to add: ");
- scanf("%d", &n);
-
- printf("\n\nEnter %d integers \n\n", n);
-
- for(c = 0; c < n; c++)
- {
- scanf("%d", &array[c]);
- sum += array[c]; // same as sum = sum + array[c]
- }
-
- printf("\n\nSum = %d\n\n", sum);
- return 0;
- }
-
- OUTPUT :
-
- Enter the number of integers you want to add: 7
- Enter 7 integers
-
- 3
- 2
- 5
- 7
- 9
- 6
- 10
- Sum = 42
-
-
➤ Simple Program to find largest and smallest Element in an Array in C .
- #include<stdio.h>
-
- int main()
- {
- int a[50], size, i, big, small;
-
- printf("\nEnter the size of the array: ");
- scanf("%d", &size);
-
- printf("\n\nEnter the %d elements of the array: \n\n", size);
- for(i = 0; i < size; i++)
- scanf("%d", &a[i]);
-
- big = a[0]; // initializing
- /*
- from 2nd element to the last element
- find the bigger element than big and
- update the value of big
- */
- for(i = 1; i < size; i++)
- {
- if(big < a[i]) // if larger value is encountered
- {
- big = a[i]; // update the value of big
- }
- }
- printf("\n\nThe largest element is: %d", big);
-
- small = a[0]; // initializing
- /*
- from 2nd element to the last element
- find the smaller element than small and
- update the value of small
- */
- for(i = 1; i < size; i++)
- {
- if(small>a[i]) // if smaller value is encountered
- {
- small = a[i]; // update the value of small
- }
- }
- printf("\n\nThe smallest element is: %d", small);
- return 0;
- }
-
-
- OUTPUT :
-
-
- Enter the size of the array: 6
- Enter the 6 elements of the array:
-
- 22
- 36
- 89
- 112
- 778
- 43
- The largest element is: 778
-
- The smallest element is: 22