Notification texts go here Contact Us Click here

Using C program to perform an operation in an Array .

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated






➤  Program to print the reverse of an Array

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     printf("\n\n\t\tFRIDAY - Best place to learn\n\n\n");
  6.  
  7.     int c, d, n, a[100], b[100];
  8.     printf("\n\nEnter number of elements in array : ");
  9.     scanf("%d", &n);
  10.     printf("\n\nEnter %d elements\n", n);
  11.  
  12.     for(= 0; c < n; c++)
  13.         scanf("%d", &a[c]);
  14.  
  15.     /*
  16.         temporarily storing elements into array b 
  17.         starting from end of array a
  18.     */
  19.     for(= n-1, d = 0; c >= 0; c--, d++)
  20.         b[d] = a[c];
  21.  
  22.     /*
  23.         copying reversed array into original.
  24.         Here we are modifying original array to reverse it.
  25.     */
  26.  
  27.     for(= 0; c < n; c++)
  28.         a[c] = b[c];
  29.  
  30.     printf("\n\n Resultant array is: ");
  31.     for(= 0; c < n; c++)
  32.         printf("%d", a[c]);
  33.  
  34.     return 0;
  35. }
  36.  
  37. OUTPUT :
  38.  
  39.  
  40. FRIDAY - Best place to learn
  41.  
  42.  
  43.  
  44.  
  45. Enter number of elements in array : 5
  46.  
  47.  
  48. Enter 5 elements
  49. 3
  50. 4
  51. 2
  52. 4
  53. 5
  54.  
  55.  
  56.  Resultant array is: 54243


➤  Program to insert an element in an Array .


  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int array[100], position, c, n, value;
  6.  
  7.     printf("\n\nEnter number of elements in array: ");
  8.     scanf("%d", &n);
  9.  
  10.     printf("\n\nEnter %d elements\n", n);
  11.     for(= 0; c < n; c++)
  12.         scanf("%d", &array[c]);
  13.  
  14.     printf("\n\nEnter the location where you want to insert new element:  ");
  15.     scanf("%d", &position);
  16.  
  17.     printf("\n\nEnter the value to insert: ");
  18.     scanf("%d", &value);
  19.  
  20.     // shifting the elements from (position to n) to right
  21.     for(= n-1; c >= position-1; c--)
  22.         array[c+1] = array[c];
  23.  
  24.     array[position - 1] = value;    // inserting the given value
  25.  
  26.     printf("\n\nResultant array is: ");
  27.     /* 
  28.         the array size gets increased by 1 
  29.         after insertion of the element
  30.     */
  31.     for(= 0; c <= n; c++) 
  32.         printf("%d  ", array[c]);
  33.  
  34.     return 0;
  35. }
  36.  
  37.  
  38. OUTPUT :
  39.  
  40. Enter number of elements in array: 5
  41. Enter 5 elements
  42. 2
  43. 3
  44. 4
  45. 5
  46. 6
  47. Enter the location where you want to insert new element:  1
  48. Enter the value to insert: 8
  49. Resultant array is: 8  2  3  4  5  6  
  50.  
  51. Again execute code :
  52. Enter number of elements in array: 5
  53. Enter 5 elements
  54. 1
  55. 2
  56. 3
  57. 4
  58. 5
  59. Enter the location where you want to insert new element:  6
  60. Enter the value to insert: 6
  61. Resultant array is: 1  2  3  4  5  6  

➤  Program to Delete an element in an Array  in C.




  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6.  
  7.     int array[100], position, c, n;
  8.     printf("\n\nEnter number of elements in array: ");
  9.     scanf("%d", &n);
  10.  
  11.     printf("\n\nEnter %d elements\n", n);
  12.     for(= 0; c < n; c++)
  13.         scanf("%d", &array[c]);
  14.  
  15.     printf("\n\nEnter the location where you want to delete element from:  ");
  16.     scanf("%d", &position);
  17.  
  18.     if(position >= n+1)
  19.         printf("\n\nDeletion not possible\n\n");
  20.     else 
  21.         // updating the locations with next elements
  22.         for(= position-1; c < n-1; c++)
  23.         array[c] = array[c+1];
  24.  
  25.     printf("\n\nResultant array is: ");
  26.     /* 
  27.         the array size gets reduced by 1 
  28.         after deletion of the element
  29.     */
  30.     for(= 0; c < n-1; c++) 
  31.         printf("%d  ", array[c]);
  32.  
  33.     return 0;
  34. }
  35.  
  36. OUTPUT :
  37.  
  38.  
  39. Enter number of elements in array: 5
  40. Enter 5 elements
  41. 11
  42. 12
  43. 13
  44. 14
  45. 15
  46. Enter the location where you want to delete element from:  2
  47. Resultant array is: 11  13  14  15  


➤  Program to Sum of N element in an Array  in C.



  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6.     int n, sum = 0, c, array[100];
  7.  
  8.     printf("Enter the number of integers you want to add: ");
  9.     scanf("%d", &n);
  10.  
  11.     printf("\n\nEnter %d integers \n\n", n);
  12.  
  13.     for(= 0; c < n; c++)
  14.     {
  15.         scanf("%d", &array[c]);
  16.         sum += array[c];    // same as sum = sum + array[c]
  17.     }
  18.  
  19.     printf("\n\nSum = %d\n\n", sum);
  20.     return 0;
  21. }
  22.  
  23. OUTPUT :
  24.  
  25. Enter the number of integers you want to add: 7
  26. Enter 7 integers 
  27.  
  28. 3
  29. 2
  30. 5
  31. 7
  32. 9
  33. 6
  34. 10
  35. Sum = 42
  36.  
  37.  



➤ Simple Program to find largest and smallest  Element in an Array in C .


  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int a[50], size, i, big, small;
  6.  
  7.     printf("\nEnter the size of the array: ");
  8.     scanf("%d", &size);
  9.  
  10.     printf("\n\nEnter the %d elements of the array: \n\n", size);
  11.     for(= 0; i < size; i++)
  12.     scanf("%d", &a[i]);
  13.  
  14.     big = a[0]; // initializing
  15.     /* 
  16.         from 2nd element to the last element 
  17.         find the bigger element than big and 
  18.         update the value of big
  19.     */
  20.     for(= 1; i < size; i++)
  21.     {
  22.         if(big < a[i])   // if larger value is encountered
  23.         {
  24.             big = a[i]; // update the value of big
  25.         }
  26.     }
  27.     printf("\n\nThe largest element is: %d", big);
  28.  
  29.     small = a[0];   // initializing
  30.     /*
  31.         from 2nd element to the last element 
  32.         find the smaller element than small and 
  33.         update the value of small
  34.     */
  35.     for(= 1; i < size; i++)
  36.     {
  37.         if(small>a[i])   // if smaller value is encountered
  38.         {
  39.             small = a[i];   // update the value of small
  40.         }
  41.     }
  42.     printf("\n\nThe smallest element is: %d", small);
  43.     return 0;
  44. }
  45.  
  46.  
  47. OUTPUT :
  48.  
  49.  
  50. Enter the size of the array: 6
  51. Enter the 6 elements of the array: 
  52.  
  53. 22
  54. 36
  55. 89
  56. 112
  57. 778
  58. 43
  59. The largest element is: 778
  60.  
  61. The smallest element is: 22

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.