bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

assignment-s-12.c (925B)


      1 /*
      2  * Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 17 Dec 2025
      3  * Repo: https://github.com/notamitgamer/bsc
      4  * License: MIT
      5  */
      6 
      7 /* Write a program to find the sum of n elements entered by the user. Use dynamic
      8 memory allocation (malloc() or calloc()). */
      9 
     10 #include <stdio.h>
     11 #include <stdlib.h>
     12 
     13 void inputarr(int[], int);
     14 int sum_elem(int[], int);
     15 
     16 int main()
     17 {
     18     int n, *arr;
     19     printf("How many element do you want to enter: ");
     20     scanf("%d", &n);
     21     arr = (int *)malloc(n * sizeof(int));
     22     inputarr(arr, n);
     23     printf("\nSum of the %d element(s) = %d", n, sum_elem(arr, n));
     24     free(arr);
     25     return 0;
     26 }
     27 
     28 void inputarr(int arr[], int n)
     29 {
     30     int i;
     31     for (i = 0; i < n; i++)
     32     {
     33         printf("Enter element %d: ", i + 1);
     34         scanf("%d", &arr[i]);
     35     }
     36 }
     37 
     38 int sum_elem(int arr[], int n)
     39 {
     40     int i, sum = 0;
     41     for (i = 0; i < n; i++)
     42     {
     43         sum += arr[i];
     44     }
     45     return sum;
     46 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror