array simple


Submit solution

Points: 15 (partial)
Time limit: 5.0s
Memory limit: 64M

Authors:
Problem type
Allowed languages
C

Task: Read an array of n elements, find the sum of all array elements.

sample input:

5 //Enter the no of elements:

10 //element1

20 //element2

30 //element3

40 //element4

50 //element5

sample output:

The array elements are 10,20,30,40,50,

sum is 150


Comments


  • 0
    cbenu4ece22126  commented on May 24, 2023, 3:28 p.m.

    include<stdio.h>

    int main() { int size,i,sum=0; printf("Enter the size of the array :\n"); scanf("%d",&size); int arr[size]; printf("Enter the elements of the array :\n"); for(i=0;i<size;i=i+1) { scanf("%d",&arr[i]); } printf("The sum of elements of the array is :\n"); for(i=0;i<size;i=i+1) { sum=sum+arr[i]; } printf("%d",sum); return 0; }


  • 0
    iamrk  commented on April 12, 2023, 11:48 a.m.

    Why is this described as the incorrect solution ?

    #include<stdio.h>
    
    int main(){
        int size, sum = 0;
        scanf("%d", &size);
        int arr[size];
    
        // Getting inputs
        for(int i = 0; i < size; i--){
            scanf("%d", &arr[i]);
        }
        // Printing array elements
        printf("The array elements are ");
        for(int i = 0; i < size; i--){
            sum = sum + arr[i];
            printf("%d", arr[i]);
        }
    
        printf("\n");
        printf("sum is %d", sum);
    
        return(0);
    }

  • 0
    Sai_1Siddharth  commented on Sept. 14, 2022, 4:43 p.m.

    include <stdio.h>

    int main() { int n,sum,i; scanf("%d",&n); int arr[n]; sum=0; for (i=0;i<n;i++) { scanf("%d",&arr[i]); } printf ("The array elements are "); for (i=0;i<n;i++) { printf ("%d,",arr[i]); } for (i=0;i<n;i++) { sum = sum + arr[i]; } printf("\n the sum is %d",sum); return 0; } //SHOWING THIS SOLUTION AS WRONG.WHY? //PLEASE CORRECT THIS