Illustrate the use of 1-D arrays in C

Fill an array, print out values and sum the elements of the array.

#include <stdio.h>
 
 int main()
 {
    int a[6];    // Space for a[0],a[1], ... , a[5] is allocated
    int i,Sum=0;
 
    /*Fill the array*/
    for(i=0;i<=5;i++)
        a[i]=i*i;
    /*Print the values in the array*/
    printf("n");
    for(i=0;i<=5;i++)
        printf("a[%d]=%d.n",i,a[i]);
    /*Calculate the sum of the values of the elements in the array*/
    for(i=0;i<=5;i++)
        Sum+=a[i];
    printf("The Sum is %d.n",Sum);
    return 0;
 }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.