Posts

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; }

wwrite a program to enter number of digits and form a number using these digits.

Image
#include<stdio.h> #include<math.h> int main() { int n,i,a[10],s=0; printf("enter the no of digit "); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the %d th digit ",i); scanf("%d",&a[i]); } i=0; while(i<n) { s=s+a[i]*pow(10,i); i++; } printf("the no is %d",s); return 0; }  

write a program to concatenate two string without using stract().

Image
#include<stdio.h> #include<conio.h> int main() { char str1[50],str2[50],i,j; printf("enter first string\n"); scanf("%s",&str1); printf("enter second string\n"); scanf("%s",&str2); for(i=0;str1[i]!='\0';i++); for(j=0;str2[j]!='\0';j++,i++) { str1[i]=str2[j]; } str1[i]='\0'; printf("\n output is %s",str1); return 0; }