This blog is under construction

Thursday, 25 July 2013

C program to increment all array elements by n and print the resultant array

Write a C program to increment all array elements by N and print the resultant array.


  #include <stdio.h>
  #define MAX 256

  int main() {
        int i, n, incr, array[MAX];

        /* get the number of array elements from user */
        printf("Enter the number of array elements:");
        scanf("%d", &n);

        /* get the array entries */
        printf("Enter your array elements:\n");
        for (i = 0; i < n; i++) {
                printf("Array[%d]: ", i);
                scanf("%d", &array[i]);
        }

        /* get the input to increment all array elements */
        printf("Increment all elements in array by ");
        scanf("%d", &incr);

        /* increment all array elements by the above given value */
        for (i = 0; i < n; i++) {
                array[i] = array[i] + incr;
        }

        /* print the resultant array */
        printf("Resultant Array:\n");
        for (i = 0; i < n; i++) {
                printf("%d ", array[i]);
        }

        printf("\n");

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of array elements:5
  Enter your array elements:
  Array[0]: 10
  Array[1]: 20
  Array[2]: 30
  Array[3]: 40
  Array[4]: 50
  Increment all elements in array by 22
  Resultant Array:
  32 42 52 62 72 


No comments:

Post a Comment