Posts

Bit printing Pattern using C

Image
#include<stdio.h> int main() { int h,i,j,k=1; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<=i;j++)   {     printf("%d ",k++%2);//1  0  1  0   }  printf("\n"); } }

another Bit printing Pattern Using C

Image
#include<stdio.h> int main() { int h,i,j; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<=i;j++)   {     printf("%d ",j%2);   }  printf("\n"); } }

print diamond with space Pattern using C

Image
#include<stdio.h> int main() { int h,i,j; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<20-i;j++)   printf("  ");  for(j=1;j<=i;j++)   {   if(j==1 || j==i)   printf("%c ",64+j);   else   printf("  ");   }  for(j=j-2;j>=1;j--)   {   if(j==1 || j==j-2)    printf("%c ",64+j);   else    printf("  ");   }  printf("\n"); } for(i=h-1;i>=1;i--) {  for(j=1;j<20-i;j++)   printf("  ");  for(j=1;j<=i;j++)   {   if(j==1 || j==i)   printf("%c ",64+j);   else   printf("  ");   }  for(j=j-2;j>=1;j--)   {   if(j==1 || j==j-2)   printf("%c ",64+j);   else   printf("  ");   }  printf("\n"); } }

print diamond of alphabets Pattern Using C

Image
//pattern #include<stdio.h> int main() { int h,i,j; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<20-i;j++)   printf("  ");  for(j=1;j<=i;j++)   printf("%c ",64+j);  for(j=j-2;j>=1;j--)   printf("%c ",64+j);  printf("\n"); } for(i=h-1;i>=1;i--) {  for(j=1;j<20-i;j++)   printf("  ");  for(j=1;j<=i;j++)   printf("%c ",64+j);  for(j=j-2;j>=1;j--)   printf("%c ",64+j);  printf("\n"); } }

Triangle of alphabets Pattern using C

Image
//Pattern #include<stdio.h> int main() { int h,i,j; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<20-i;j++)   printf("  ");  for(j=1;j<=i;j++)   printf("%c ",64+j);//123//ABC  for(j=j-2;j>=1;j--)   printf("%c ",64+j);//21//BA      ABCBA  printf("\n"); } }

Pyramid of increasing then decreasing number Pattern using C

Image
//Pattern #include<stdio.h> int main() { int h,i,j; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<11-i;j++)   printf("  ");  for(j=1;j<=i;j++)   printf("%2d",j);  for(j=j-2;j>=1;j--)   printf("%2d",j);  printf("\n"); } }

star in diamond shape Pattern using C

Image
//Pattern #include<stdio.h> int main() { int h,i,j; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<40-i;j++)   printf(" ");  for(j=1;j<=i;j++)   printf("* ");  printf("\n"); } for(i=h-1;i>=1;i--)//4, 3 {  for(j=1;j<40-i;j++)//32   printf(" ");  for(j=1;j<=i;j++)//j=1..4 -----   1..3 ------  1..2 ---  1..1   printf("* ");  printf("\n"); } }

alphabets pyramid Pattern using C

Image
//Pattern #include<stdio.h> int main() { int h,i,j; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<40-i;j++)   printf(" ");  for(j=1;j<=i;j++)   printf("%c ",64+j);  printf("\n"); } }

Print Pattern using C

Image
//Pattern Print #include<stdio.h> int main() { int h,i,j; printf("Enter the height"); scanf("%d",&h); for(i=1;i<=h;i++) {  for(j=1;j<40-i;j++)//39 , 38   ,37  , 36    printf(" ");  for(j=1;j<=i;j++)   printf("%c ",64+i);  printf("\n"); } }

Pascal Triangle using 2D array

Image
//Pascal Triangle .... #include<stdio.h> int main() { int ar[10][10],m,n,i,j; printf("\nenter the height of the pascal triangle : "); scanf("%d",&m); for(i=0;i<m;i++) for(j=0;j<m;j++) { if(j==0) ar[i][j]=1; else ar[i][j]=0; } for(i=1;i<m;i++) { for(j=1;j<=i;j++) { ar[i][j]=ar[i-1][j-1]+ar[i-1][j]; } } printf("\nPscal Triangle will look like : \n"); for(i=0;i<m;i++) { for(j=0;j<20-i;j++) printf(" "); for(j=0;j<=i;j++) printf("%2d",ar[i][j]); printf("\n"); } } O/P:

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

Binary Search Tree Mirror Image creation......

/*Binary Search Tree with Mirror Image Conversion of the original Tree ... */ #include<stdio.h> #include<stdlib.h> typedef struct BinarySearchTree { struct BinarySearchTree *left; struct BinarySearchTree *right; int val; }bst; typedef struct Queue { bst *val; struct Queue *next; }qu; void enqueu(qu **,bst *); bst *dequeu(qu **); int isempty(qu *); bst * create(bst *,int); void inorder(bst *); void preorder(bst *); void postorder(bst *); void levelorder(bst *); void mirimg(bst *); int main() { bst *root=NULL; int g,ch; while(1) { printf("\n\n 1) Insert\n 2) Preorder traversal\n 3) Inorder traversal\n 4) Postorder traversal\n 5) Level Order Traversal\n 6) Construct Mirro Image of the tree\n 7) Exit"); printf("\n\n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nInsert value for the tree: "); scanf("%d",&g); root=create(root,g); break; c...

Binary Search Tree With Level Order Traversal

Image
/*Binary Search Tree with level order traversal ... Queue for level order traversal is implemented using linked list*/ #include<stdio.h> #include<stdlib.h> typedef struct BinarySearchTree { struct BinarySearchTree *left; struct BinarySearchTree *right; int val; }bst; typedef struct Queue { bst *val; struct Queue *next; }qu; void enqueu(qu **,bst *); bst *dequeu(qu **); int isempty(qu *); bst * create(bst *,int); void inorder(bst *); void preorder(bst *); void postorder(bst *); void levelorder(bst *); int main() { bst *root=NULL; int g,ch; while(1) { printf("\n 1) Insert\n 2) Preorder traversal\n 3) Inorder traversal\n 4) Postorder traversal\n 5) Level Order Traversal\n 6) Exit"); printf("\n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nInsert value for the tree: "); scanf("%d",&g); root=create(root,g); break; case 2: printf("\nValues in...

Different functions of Linked List using menu driven approach

/*insertend(),inserbef(),insertatpos() functions are used to create a linked using its position value,del() function will delete the element you have specified,disp() function is used to display the elements in the linked list*/ #include<stdio.h> #include<stdlib.h> typedef struct Linked { int val; struct Linked *next; }lnk; lnk* insertbeg(lnk *,int); lnk* insertend(lnk *,int); lnk *insertatpos(lnk *,int,int); lnk *del(lnk *,int); void disp(lnk *); int main() { lnk *head=NULL; int ch,v,p; while(1) { printf("\n1)Insert End\n2) Insert begining\n3)Insert at Position\n4)Delete\n5)Display\n6)Exit"); printf("\n Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the value to be inserted : ");  scanf("%d",&v);  head=insertend(head,v);  break; case 2: printf("\nEnter the value to be inserted : ");  scanf("%d",&v);  head=insertbeg(head,v);  b...

Linked list created and there after elements are displayed

Image
#include<stdio.h> #include<stdlib.h> typedef struct Linked { int val; struct Linked *next; }lnk; lnk* createlink(); void disp(); int main() { lnk *head; head=createlink(); printf("Values in the linked list :: "); disp(head); } lnk *createlink() { lnk *h=NULL,*tmp,*ptr; char ch; int v; while(1) { printf("\nEnter the value for the node (enter -99 to exit) : "); scanf("%d",&v); if(v==-99) return h; tmp=(lnk *)malloc(sizeof(lnk)); tmp->val=v; tmp->next=NULL; if(h==NULL) h=tmp; else ptr->next=tmp; ptr=tmp; } } void disp(lnk *h) { while(h!=NULL) { printf("%d,",h->val); h=h->next; } printf("\b"); }

Circular Queue using Structure

Image
#include<stdio.h> #include<stdlib.h> #define max 5 typedef struct Queue { int val[max]; int rear,front; }qu; void enque(qu *,int); int deque(qu*); int peek(qu); int isempty(qu); int isfull(qu); void disp(qu); void main() { qu q; int g,v; q.rear=q.front=-1; while(1) { printf("\n1) Insert\n2) Delete\n3) Peek\n4) Display\n5) Exit"); printf("\n Enter your choice :"); scanf("%d",&g); switch(g) { case 1: printf("\n Enter the value"); scanf("%d",&v); if(!isfull(q)) enque(&q,v); else printf("\nNo more space"); break; case 2: if(!isempty(q)) printf("\nDeleted element is %d",deque(&q)); else printf("\nNothing to delete"); break; case 3: if(!isempty(q)) printf("\n value at front %d",peek(q)); else printf("\n Nothing in the front"); break; case 4: if(!isempty(q)) { printf("\nValues int queue...

Linear Queue using Structure

Image
#include<stdio.h> #include<stdlib.h> #define max 50 typedef struct Queue { int val[max]; int front,rear; }qu; void enqueu(qu *,int); int dequeu(qu *); int peek(qu); int isempty(qu); int isfull(qu); void disp(qu); int main() { int ch,v; qu q; q.front=q.rear=-1; while(1) { printf("\n1) Insert \n2) Delete \n3)Front value\n4)Display\n5) Exit"); printf("\n Enter the choice : "); scanf("%d",&ch); switch(ch) { case 1: if(!isfull(q)) { printf("\n Enter the value : "); scanf("%d",&v); enqueu(&q,v); } else printf("\nNo more space"); break; case 2: if(!isempty(q)) { v=dequeu(&q); printf("\nThe deleted value from the front is %d",v); } else printf("\nNothing to delete"); break; case 3: if(!isempty(q)) { printf("\nThe Value at the front is %d",peek(q)); } else printf("\nNothing in the queue"); br...

Infix to Postfix conversion along with Postfix evaluation

#include<stdio.h> #include<conio.h> #define max 50 typedef struct stack { char ar[max]; int top; }st; typedef struct stack1 { float res[max]; int top; }st1; void push(st *,char); void push1(st1 *,float); char pop(st *); float pop1(st1 *); char peek(st); int isfull(st); int isempty(st); float pfixeval(char []); void intopost(char [],char []); int getpriority(char); int main() { char expr[max],res[max]; float p; printf("\nEnter the expression (in digits only) to be evaluated in Infix form and rest is our responsibility : "); gets(expr); intopost(expr,res); printf("\nThe postfix expression is : "); puts(res); p=pfixeval(res); printf("\nEvaluated result is : %f",p); getch(); } void intopost(char expr[],char res[]) { st s; char ch; int i,j; s.top=-1; for(i=0,j=0;expr[i]!='\0';i++) { if(expr[i]==' ') continue; else if(expr[i]=='(' || expr[i]=='{' || expr[i]=='[...