Friday, 6 February 2015

pattern 4

1 2*2 3*3*3 4*4*4*4 4*4*4*4 3*3*3 2*2 1 #include <stdio.h> void draw_line(int n); void draw_pattern(int n,int m); void draw_rpattern(int n,int m); void main() { draw_pattern(4,4); } void draw_line(int n) { int i; for (i=0;i<n-1;i++ ) { printf("%d*",n); } printf("%d\n",n); } void draw_pattern(int n,int m) { if(n==1) draw_line(n); else { draw_pattern(n-1,m); draw_line(n); if(n==m) draw_rpattern(n,m); } } void draw_rpattern(int n,int m) { if(n==1) draw_line(n); else { draw_line(n); draw_rpattern(n-1,m); } }

pattern 3

if n=7 * * * * * * * * * * * * * * * * * * * * * * * * * #include <stdio.h> void draw_line(int nst,int n) { int i; int nsp=(n-nst)/2; for (i=0;i<nsp;i++) printf(" "); for (i=0;i<nst;i++) printf("*"); printf("\n"); } void draw_rpattern(int nst,int n) { if(nst==1) draw_line(nst,n); else { draw_line(nst,n); draw_rpattern(nst-2,n); } } void draw_pattern(int nst,int n) { if(nst==1) draw_line(nst,n); else { draw_pattern(nst-2,n); draw_line(nst,n); if(nst==n) draw_rpattern(nst-2,n); } } void main() { draw_pattern(7,7); }

pattern 2 program

if n=7 * * * * * * * * * * * * * * * * #include <stdio.h> void draw_line(int nst,int n) { int i; int nsp=(n-nst)/2; for (i=0;i<nsp;i++) printf(" "); for (i=0;i<nst;i++) printf("*"); printf("\n"); } void draw_pattern(int nst,int n) { if(nst==1) draw_line(nst,n); else { draw_pattern(nst-2,n); draw_line(nst,n); } } void main() { draw_pattern(7,7); }

pattern program 1

1 3*2 4*5*6 10*9*8*7 11*12*13*14*15 #include <stdio.h> int draw_line(int flag,int val,int count) { int i,k; if(!flag) { for (i=0,k=val ;i< count-1;i++ ) { printf("%d*",k); k++; } printf("%d\n",k); return k; } else { int hval=val+count-1; for (i=0,k=hval;i<count-1;i++,k--) { printf("%d*",k); } printf("%d\n",k); return hval; } } int draw_pattern(int n) { int r; if(n==1) { printf("%d\n",n); return n; } r=draw_pattern(n-1); if(n%2) return draw_line(0,r+1,n); else return draw_line(1,r+1,n); } void main() { draw_pattern(6); }

Create a Single Linked List program using C

#include struct Node { int data; struct Node *next; }; struct Node *create_list(int *ptr,int n) { struct Node *hptr=NULL,*tptr,*temp; int i; if(n>0) { hptr=(struct Node*)malloc(sizeof(struct Node)); tptr=hptr; hptr->data=ptr[0]; for (i=1;idata=ptr[i]; temp->next=NULL; tptr->next=temp; tptr=temp; } } return hptr; } void display(struct Node *dptr) { struct Node *temp; for (temp=dptr;temp!=NULL ;temp=temp->next ) { printf("%d\n",temp->data); } } struct Node *getNfromLast(struct Node *hptr,int n) { struct Node *fptr,*bptr; int i; fptr=hptr; bptr=hptr; for (i=0;inext; while(fptr->next!=NULL) { fptr=fptr->next; bptr=bptr->next; } return bptr; } void main() { int arr[]={1,2,3,4,5,6}; struct Node *hptr,*tptr; hptr=create_list(arr,6); display(hptr); tptr=getNfromLast(hptr,3); printf("\n%d\n",tptr->data); }

Create a Single Linked List using array and find an element in the list

#include <stdio.h> struct Node { int data; struct Node *next; }; struct Node *create_list(int *ptr,int n) { struct Node *hptr=NULL,*tptr,*temp; int i; if(n>0) { hptr=(struct Node*)malloc(sizeof(struct Node)); tptr=hptr; hptr->data=ptr[0]; for (i=1;i<n;i++) { temp=(struct Node *)malloc(sizeof(struct Node)); temp->data=ptr[i]; temp->next=NULL; tptr->next=temp; tptr=temp; } } return hptr; } char* rfind(struct Node *hptr,int d) { if(hptr==NULL) return "NOT FOUND"; else if(hptr->data==d) return "FOUND"; else return rfind(hptr->next,d); } void main() { int arr[]={10,12,53,84,35,16}; struct Node *hptr,*tptr; hptr=create_list(arr,6); printf("%s\n",rfind(hptr,45)); }

Find count the number of occurrence of Strings in a Sentence?

Hint: input:this is String this is String output:[this 2 is 2 String 2] import java.util.*; public class FC { public static void main(String[] args) { HashMap<String,Integer> hm=new HashMap<String,Integer>(); for(String s:args) { if(hm.get(s)!=null) { int v=hm.get(s); v=v+1; hm.put(s,v); } else hm.put(s,1); } System.out.println(hm); } }