The sum of all the elements of an array of size 4.


  1. Write a C program to print the sum of all the elements of an array of size 4.


Instructions:

  • Define an integer array numArray[4].
  • Get the input for the array elements.
  • Write the logic to add the elements.
  • Print the sum.

Input:


#include <stdio.h>
int main(void) {
int numArray[4];   // Define an array of four integers
      int i;
                           // Get inputs for the array elements
      for (i=0; i<4; i++) {
    scanf("%d", &numArray[i]);
}
      int sum = 0;
                            // Write the logic to add these integers here:
    
       for(i=0;i<4;i++) sum +=numArray[i];
    
      printf("%d\n",sum);   // Print the sum
    
return 0;
}


Let see the Computer Screen:


Output:

4
5
6
8
23

--------------------------------
Process exited after 10.48 seconds with return value 0







0 comments :

Post a Comment

Cancel Reply

computer books list list of computer science books with authors best computer books for beginners fundamental of computer books pdf free download computer books for dummies best computer hardware books introduction to computer best computer books of all time

Powered by Blogger.

Followers

Search This Blog

Translate