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

  1. Write a program to print the sum of all the elements of an array of size N where N can be any integer between 1 and 100. [1<=N<=100]


Instructions:

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

Input:

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

Let see The Computer Screen:


Output:

Where N=3













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