Sort by

recency

|

488 Discussions

|

  • + 0 comments

    int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
        int i,a[10]={16,13,7,2,1,12};
     int n=6,sum=0;
    
    
    
          for(i=0;i<n;i++)
         {
          sum+=a[i];
                    }
                    printf(" %d",sum);
                    return 0;
    
                    }
    
        why is dosnt work?
    
    
    
    
    
          printf("%d",sum);
    
    return 0;
    

    }

  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
    
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
        int n; 
        scanf("%d", &n);
        
        int arr[n];
        for (int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += arr[i];
        }
        printf("%d", sum);  
        return 0;
    }
    
  • + 0 comments

    Try this code

        int n;
        scanf("%d",&n); // input
        int sum = 0;
        int *arr = (int*) malloc(n * sizeof(int)); // using the malloc function and the memory is allocated on the heap at runtime
        for (int i = 0 ; i < n; i++){
            scanf("%d",&arr[i]);
            sum += arr[i];
        }
        
        printf("%d",sum); // printing the sum
        
        free(arr); // you have finished with the array, use free(arr) to deallocate the memory.
        arr = NULL;
          
        return 0;
    }
    
  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    int getsum(int n)
    {
        int a[1000],sum=0;
        for (int i=0;i<n;i++)
        {
            scanf("%d ",&a[i]);
            sum=sum+a[i];
        }
        printf("%d",sum);
        return 0;
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        getsum(n);
    }
    
  • + 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;
    

    }