Posts

Showing posts from 2019

Add two very large number using linked list

/*this program will add two large number(any number of digits)*/ #include<stdio.h> #include<stdlib.h> typedef struct linked { int val; struct linked *next; }lnk; lnk *create(); lnk *add(lnk *,lnk *); void disp(lnk *); int main() { lnk *h1,*h2,*h3; printf("\n\nEnter the first large number : "); h1=create(); printf("\n\nEnter the second large number : "); h2=create(); h3=add(h1,h2); printf("\n\nResultant number : "); disp(h3); return 0; } lnk *create() { int a,m; lnk *temp,*h=NULL; char ch; while((ch=getch())!=13) { printf("%c",ch); temp=(lnk*)malloc(sizeof(lnk)); temp->val=ch-'0'; temp->next=h; h=temp; } return h; } lnk *add(lnk *h1,lnk *h2) { lnk *h3=NULL,*temp,*ptr; int cr,t=0; while(h1!=NULL && h2!=NULL) { t=h1->val+h2->val+t; cr=t%10; temp=(lnk *)malloc(sizeof(lnk)); temp->val=cr; temp->next=h3; h3=temp; t=t/10; h1=h1->next; h2=h2->next; } if(h1!=NULL && h2==NULL) { while(h1!=NULL)

Insertion sort

Image
/* Sort an array using Insertion sort */ #include<stdio.h> int main() { int a[100],n,m,i,t; printf("Enter the number of elements : "); scanf("%d",&m); printf("Enter the values : "); for(i=0;i<m;i++) { scanf("%d",&a[i]); } printf("\nValues in the array are : "); for(i=0;i<m;i++) printf("%d,",a[i]); for(i=0;i<m;i++) { t=i-1; n=a[i]; while(t>=0 && a[t]>n) { a[t+1]=a[t]; t--; } a[t+1]=n; } printf("\nAfter arrangement : "); for(i=0;i<m;i++) printf("%d,",a[i]); printf("\n\n\n"); }

Array rearrangement

Image
/* Arrange the elements of the array in such a way so that negative elements will be in one side and positive elements will be in another side. But don't hamper its respective order */ #include<stdio.h> int main() { int a[7],n,m,i,t; printf("Enter the number of elements : "); scanf("%d",&m); printf("Enter the values : "); for(i=0;i<m;i++) { scanf("%d",&a[i]); } printf("\nValues in the array are : "); for(i=0;i<m;i++) printf("%d,",a[i]); for(i=0;i<m;i++) { if(a[i]<0) { t=i-1; n=a[i]; while(t>=0 && a[t]>0) { a[t+1]=a[t]; t--; } a[t+1]=n; } } printf("\nAfter arrangement : "); for(i=0;i<m;i++) printf("%d,",a[i]); printf("\n\n\n"); }