/* */ Click to Join Live Class with Shankar sir Call 9798158723
Q.) WAP to find sum of all elements of an array With C program.
#include < stdio.h>
#define MAX_SIZE 100

int main()
{
    int arr[MAX_SIZE];
    int i, n, sum=0;

   
    printf("Enter size of the array: ");
    scanf("%d", &n);
    printf("\nEnter %d elements in the array: \n", n);
    for(i=0; i< n; i++)
    {
        printf("\nEnter %d element in the array: ", i+1);
        scanf("%d", &arr[i]);
    }

    
    for(i=0; i< n; i++)
    {
        sum = sum + arr[i];
    }

    printf("\nSum of all elements of array = %d \n", sum);

    getch();
}
Output
Enter size of the array: 3
Enter 3 elements in the array:
Enter 1 element in the array: 1
Enter 2 element in the array: 2
Enter 3 element in the array: 3


Sum of all elements of array = 6



Program Explanation


Step 1 : Include header files (#include< stdio.h> and #include< conio.h>).

Step 2 : Start with main function with return type.

Step 3 : parenthesis to start and end the program { }.

Step 4 : declare variables with data type i.e, 'arr[MAX_SIZE],i, n, sum=0' is an integer type so we use "int" data type.

Step 5 : Use output function printf() to print the output on the screen.

Step 6 : Use input function scanf() to get input from the user.

Step 7 : here, we have find sum of all elements of an array, Input the size of the array means "enter the value of n" then Input the array elements and Adds each element of array and then output the "Sum of all elements of array".

Step 8 : using getch() function to hold the screen.