Sort by

recency

|

484 Discussions

|

  • + 0 comments

    int main() { int n; scanf("%d", &n);

    const unsigned int SIZE = n;
    int* p = malloc(SIZE * sizeof(int));
    if (!p) { exit(1); }
    
    int sum = 0;
    
    for (int i = 0; i < SIZE; ++i) {
        scanf("%d", p + i);
        sum += p[i];
    }
    printf("%d\n", sum);
    
    free(p);
    p = NULL;
    
    return 0;
    

    }

  • + 0 comments

    int n;

    scanf("%d",&n);
    
    n++;
    
    int* arr=(int*)malloc(n * sizeof(int));
    
    arr[0]=0;
    
    for(int i=1; i<n; i++){
    scanf("%d",arr+i);
    arr[0]+=arr[i];
    if(i==n-1)
    printf("%d",arr[0]);
    }
    free(arr);
    
  • + 0 comments

    How can we take those 'n' inputs using spaces in between them as shown in the sample input?

  • + 1 comment
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() 
    {
        int n;
        scanf("%d", &n);
    
        // Create a dynamic array of size n
        int* arr = (int*)malloc(n * sizeof(int));
    
        // Read the values from stdin and store them in the array
        for (int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }
    
        // Calculate the sum of all elements in the array
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += arr[i];
        }
    
        // Print the sum
        printf("%d\n", sum);
    
        // Free the memory where the array is stored
        free(arr);
    
        return 0;
    }
    
  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
        int a,b[1000],sum=0;
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
       scanf("%d",&a);
        for(int i=0;i<a;i++)
        {
            scanf("%d", &b[i]); 
            sum+=b[i];
        }
        printf("%d",sum);
        return 0;
    }