Saturday, July 14, 2012

Pattern Diamond

Write a program to output Diamond?

1:  #include<stdio.h>  
2:  #define PARA 12  
3:  int main() {  
4:       int i,d;  
5:       i = (PARA/2);  
6:       d = (PARA/2);  
7:       int row,col;  
8:       for(row = 0 ; row <= (PARA/2) ; row++) {  
9:               for(col = 0 ; col <= PARA ; col++) {  
10:                        if(col == i || col == d) {  
11:                                 printf("*");  
12:                        }  
13:                        else {  
14:                                 printf(" ");  
15:                        }  
16:               }  
17:               i++;  
18:               d--;  
19:               printf("\n");  
20:       }  
21:       i = 1;  
22:       d = (PARA-1);  
23:       for(row = 0 ; row < (PARA/2) ; row++) {  
24:                for(col = 0 ; col <= PARA ; col++) {  
25:                      if(col == i || col == d) {  
26:                              printf("*");  
27:                      }  
28:                      else {  
29:                              printf(" ");  
30:                      }  
31:                }  
32:                i++;  
33:                d--;  
34:                printf("\n");  
35:       }  
36:  }  

Output:-









Note:- You can increase or decrease the size of diamond. For this just change the value of MACRO "PARA"
i.e #define PARA 12
But the value of PARA should be EVEN....

Friday, July 13, 2012

Pattern English Alphabet X

Write a Program to output this pattern English Alphabet X?

1:  #include<stdio.h>  
2:  int main() {  
3:     int x = 0,y = 5,row,col;  
4:     for(row = 0 ; row < 6 ; row++) {  
5:        for(col = 0 ; col < 6 ; col++) {  
6:          if(col == x || col == y) {  
7:             printf("*");  
8:          }  
9:          else {  
10:             printf(" ");  
11:          }  
12:        }  
13:        x++;  
14:        y--;  
15:        printf("\n");  
16:     }  
17:  }  

Output:-



Pattern English Alphabet Z

Write a Program to output this pattern English Alphabet Z?


                              
1:  #include<stdio.h>  
2:  int main() {  
3:    int x = 5,row,col;  
4:    for(row = 0 ; row < 6 ; row++) {  
5:        for(col = 0 ; col < 6 ; col++) {  
6:            if(row == 0 || row == 5) {  
7:                printf("* ");  
8:            }  
9:            else {  
10:                if(col == x) {  
11:                  printf("* ");  
12:                }  
13:                else {  
14:                  printf(" ");  
15:                }  
16:            }  
17:         }  
18:         x--;  
19:         printf("\n");  
20:     }  
21:  }  

Output:-



Thursday, July 12, 2012

Pattern Square

Write a program to output this pattern in C?



1:  #include<stdio.h>  
2:  int main() {  
3:       int col,row;  
4:       for(row = 0 ; row < 5 ; row++) {  
5:             for(col = 0 ; col < 5 ; col++) {  
6:                 if(row == 0 || row == 4) {  
7:                         printf("* ");  
8:                  }  
9:                  else {  
10:                        if(col == 0 || col == 4) {  
11:                            printf("* ");  
12:                         }  
13:                         else {  
14:                             printf(" ");  
15:                         }  
16:                  }  
17:             }  
18:             printf("\n");  
19:        }  
20:  }   

Output:-  






Note:- You can change the value of row and col to increase or decrease the size of pattern...  

Wednesday, July 4, 2012

Calculate CRC

Program to calculate CRC in C language.......

1:   #include<stdio.h>  
2:   #define MS 14  
3:  // MS Message Size in Bits  
4:  // DS Divisor Size in Bits  
5:  int main() {  
6:     int divisor4[50] = {1,0,0,1,1};  
7:     int divisor24[50] = {1,0,1,0,1,1,1,0,1,0,1,1,0,1,1,0,1,1,1,0,0,1,0,1,1 };  
8:     int message[150] = {1,1,0,0,0,1,1,0,1,0,1,1,0,1};  
9:     int msgcopy[150];  
10:     int divisor[50];  
11:     int i,j,DS;  
12:     printf("At sender side\n");  
13:     printf("Enter the CRC you want to use in Bits 4Bit,8Bit,16Bit,24Bit,32Bit\n");  
14:     scanf("%d",&DS);  
15:   // Checking the Size of the divisor in bits  
16:     if(DS == 4) {  
17:         for(i = 0 ; i < DS+1 ; i++) {  
18:               divisor[i] = divisor4[i];  
19:          }  
20:     }  
21:     else if(DS == 24) {  
22:          for(i = 0 ;i < DS+1 ; i++) {  
23:                divisor[i] = divisor24[i];  
24:           }  
25:     }  
26:    // Copying message to another array  
27:     for(i = 0 ; i < MS ; i++) {  
28:           msgcopy[i] = message[i];  
29:      }  
30:    // Appending zero's at the end of the message  
31:     for(i = MS ; i < MS+DS-1 ; i++) {  
32:           message[MS] = 0;  
33:      }  
34:    // Calculating CRC  
35:      i = 0;  
36:      while(i < MS) {  
37:         if(message[i] == 0) {  
38:            i++;  
39:         }  
40:         else {  
41:            for(j = 0 ; j < DS+1 ; j++) {  
42:                  message[i+j] = message[i+j]^divisor[j];  
43:               //  printf("message[%d+%d] = %d\n",i,j,message[i+j]);  
44:             }  
45:          }  
46:      }  
47:     //Appending CRC at the end of the message  
48:      printf("CRC is\n");  
49:      for(i = MS ; i < MS+DS ; i++) {  
50:              msgcopy[i] = message[i];  
51:              printf("msgcopy[%d] = %d\n",i,msgcopy[i]);  
52:      }  
53:     printf("Message after appending CRC at the end\n");  
54:     for(i = 0 ; i < MS+DS ; i++) {  
55:              printf("msgcopy[%d] = %d\n",i,msgcopy[i]);  
56:      }  
57:      for(i = 0 ; i < MS+DS ; i++) {  
58:              message[i] = msgcopy[i];  
59:      }  
60:    // Checking that CRC is correct or not  
61:     printf("At receiver end\nChecking that CRC is correct or not\n");  
62:     i = 0;  
63:     while(i < MS+DS) {  
64:             if(message[i] == 0) {  
65:               i++;  
66:             }  
67:             else {  
68:               for(j = 0 ; j < DS+1 ; j++) {  
69:                   message[i+j] = message[i+j]^divisor[j];  
70:                //  printf("message[%d+%d] = %d\n",i,j,message[i+j]);  
71:               }  
72:            }  
73:      }  
74:   // CRC, After appending CRC at the end of the message  
75:     printf("CRC after appending Original CRC at the end of the message\n");  
76:     for(i = MS ; i < MS+DS ; i++) {  
77:              printf("message[%d] = %d\n",i,message[i]);  
78:     }  
79:  }  
Output :-
1:  At sender side  
2:  Enter the CRC you want to use in Bits 4Bit,24Bit  
3:  4  
4:  CRC is  
5:  msgcopy[14] = 1  
6:  msgcopy[15] = 0  
7:  msgcopy[16] = 0  
8:  msgcopy[17] = 1  
9:  Message after appending CRC at the end  
10:  msgcopy[0] = 1  
11:  msgcopy[1] = 1  
12:  msgcopy[2] = 0  
13:  msgcopy[3] = 0  
14:  msgcopy[4] = 0  
15:  msgcopy[5] = 1  
16:  msgcopy[6] = 1  
17:  msgcopy[7] = 0  
18:  msgcopy[8] = 1  
19:  msgcopy[9] = 0  
20:  msgcopy[10] = 1  
21:  msgcopy[11] = 1  
22:  msgcopy[12] = 0  
23:  msgcopy[13] = 1  
24:  msgcopy[14] = 1  
25:  msgcopy[15] = 0  
26:  msgcopy[16] = 0  
27:  msgcopy[17] = 1  
28:  At receiver end  
29:  Checking that CRC is correct or not  
30:  CRC after appending Original CRC at the end of the message  
31:  message[14] = 0  
32:  message[15] = 0  
33:  message[16] = 0  
34:  message[17] = 0  

Note :- This is for 4 and 24 Bit polynomial if you want to calculate CRC for 8,16,32 and 64 Bits then this code requires some changes . Try it youself.........
This code is calculating CRC for 14 Bits long message, you can use it for another length but don not forget to change Macro definition i.e 

#define MS 14