The code allocates dynamic memory using malloc to store integers entered by the user. It prompts the user for the number of terms, allocates memory for an integer array of that size, collects user input into the array, prints the stored numbers and their sum, and frees the allocated memory.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
19 views
Dynamic Memory Allocation
The code allocates dynamic memory using malloc to store integers entered by the user. It prompts the user for the number of terms, allocates memory for an integer array of that size, collects user input into the array, prints the stored numbers and their sum, and frees the allocated memory.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
RegNo:412511104041
KISHORE PARTHASARATHY
Dynamic Memory Allocation Using Malloc Function
Code: #include<stdio.h> #include<stdlib.h> main() { int *p,i,k,sum=0; printf("\nenter the number of terms"); scanf("%d",&k); p=(int*)malloc(k*sizeof(int)); for(i=0;i<k;i++) { printf("\nenter the number %d . ",i+1); scanf("%d",p+i); } printf("\nthe numbers are :"); for(i=0;i<k;i++) { printf("\n %d. %d",(i+1),*(p+i)); sum+=*(p+i); } printf("\nthe sum= %d",sum); free(p); } Output: enter the number of terms5 enter the number 1 . 45 enter the number 2 . 78 enter the number 3 . 96 enter the number 4 . 12 enter the number 5 . 23 the numbers are : 1. 45 2. 78 3. 96 4. 12 5. 23 the sum= 254