Posts

Showing posts from August, 2021

write a program to de-allocate the memory then free the allocated memory.

Image
#include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { int n,i; int *ptr; printf("enter the no. of elements:"); scanf("%d",&n); ptr=(int*)malloc(n*sizeof(int)); printf("memory allocated using malloc\n"); for(i=0;i<n;i++) { ptr[i]=i+1; } free(ptr); printf("\nmemory freed"); return 0; }

write a program to reallocate the memory dynamically using re-alloc method .

Image
#include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { int n,i; int *ptr; printf("enter the no. of elements:"); scanf("%d",&n); ptr=(int*)calloc(n,sizeof(int)); printf("memory allocated using calloc\n"); for(i=0;i<n;i++) { ptr[i]=i+1; } printf("elements in the array are\n"); for(i=0;i<n;i++) { printf("%d\n",ptr[i]); } printf("enter the new size:"); scanf("%d",&n); ptr=realloc(ptr,n*sizeof(int)); printf("memory reallocated using realloc\n"); for(i=0;i<n;i++) { ptr[i]=i+1; } printf("elements of array are\n"); for(i=0;i<n;i++) { printf("%d\n",ptr[i]); } return 0; }

write a program to allocate the memory dynamically using malloc.

Image
#include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { int i,n; int *ptr; printf("enter the no. of elements\n"); scanf("%d",&n); ptr=(int*)malloc(n*sizeof(int)); printf("memory allocated\n"); for(i=0;i<n;i++) { ptr[i]=i+1; } printf("elements in the array are\n"); for(i=0;i<n;i++) { printf("%d\n",ptr[i]); } return 0; }

write a program to find whether the sum of the triplet is equal to the given value.

Image
#include<stdio.h> #include<conio.h> int main() { int a[5]={4,2,3,8,12}; int i,j,k,sum=15,p=0; for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { for(k=j+1;k<5;k++) { if(a[i]+a[j]+a[k]==sum) { p=1; } } } } printf("%d",p); return 0; } #include<stdio.h> #include<conio.h> int main() { int a[5]={4,2,3,8,12}; int i,j,k,sum=25,p=0; for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { for(k=j+1;k<5;k++) { if(a[i]+a[j]+a[k]==sum) { p=1; } } } } printf("%d",p); return 0; }