Sunday, May 25, 2014

Calculate the size of an array at run time

Calculate the size of an array at run time


In c language there is no direct way to calculate the size of an array at run time;

For example:

int array[10] = {1,2,3,4,54,6,7,8,9,3}

To calculate the size of above array at run time:

int size = sizeof(array) / sizeof(array[0]);


sizeof(array) = 20 for 16 bit (2 Byte) compiler
sizeof(array) = 40 for 32 bit (4 Byte) compiler 


sizeof(array[0]) = 2 for 16 bit (2 Byte) compiler
sizeof(array[0]) = 4 for 32 bit (4 Byte) compiler


// For 16 Bit compiler
size = 20 / 2
size = 10
//For 32 Bit Compiler
size = 40 / 4 for 32 Bit
size = 10

What is sizeof() ?

By +Dixit Singla 

Saturday, November 16, 2013

Sum of primes below 5000

What is the sum of all prime numbers below 5000?

Language C++:


#include<iostream>
using namespace std;

class prime {
          private:
                int sumprime;
          public:
                prime() { 
                      sumprime = 2;
                }
                void calc_sumprime() { 
                      int i,counter = 0;
                      int num = 3;
                      while (num <= 5000) { 
                              for(i = 2 ; i < num ; i++) { 
                                      if((num % i) == 0) {
                                              counter++;
                                              break;
                                       }
                               }
                               if(counter == 0) {
                                      sumprime += num;
                               }
                               counter = 0;
                               num++;    
                       }
                       cout<<"sum = "<<sumprime<<endl;
                 }
};

int main() {
        prime obj;
        obj.calc_sumprime();
        return(0);
}

Output:
1548136

By +Dixit Singla 

Sum of first 250 prime numbers


In math, a prime number is a number only divisible by 1 and itself.

Given the first few prime numbers2 3 5 7 11 13 17 ...

What is the sum of the first 250 prime numbers? Note that you are seeing the first 7 primes above.

Language C++:

#include<iostream>
using namespace std;

class prime {
       private:
              unsigned int sum_prime;
       public:
              prime() {
              sum_prime = 2;
              }
              void sum_of_primes_calc() { 
                          int pcounter = 1;
                          int num = 3;
                          int i,counter = 0;
                          while(pcounter != 250) {
                                   for (i = 2 ; i < num ; i++) { 
                                               if((num % i) == 0) {
                                                       counter++;
                                                }
                                    }
                                    if(counter == 0) { 
                                            sum_prime += num;
                                            pcounter++;
                                    }
                                    counter = 0;
                                    num++;
                           }
                           cout<<sum_prime<<endl;
              }
};

int main() {
        prime obj;
        obj.sum_of_primes_calc();
        return(0);
}

Output:
182109

Note: To find the sum of primes beyond 250, just change the value in while condition "While(pcounter  !=  250)"

By +Dixit Singla 

Friday, November 15, 2013

Sum of digits


As an example, the sum of the digits of 2 to the 10th power is:

2^10 = 1024  => 1 + 0 + 2 + 4 => 7


What is the sum of the digits of  2^50?
Note:  ^  means exponent

#include<iostream>
using namespace std;

class sumofdigit {
 
             private:
                    long long int val;
             public:
                    sumofdigit() {
                              val = 1;
                    }
                    void calc_sumofdigit () {
                              long long int temp = 0;
                              int i,sum = 0;
                              for (i = 1 ; i <= 50 ; i++) { 
                                          val = val * 2;
                              }
                              cout<<"2^50 = "<<val<<endl;
                              while (val != 0) { 
                                          temp = val % 10;
                                          sum = sum + temp;
                                          val = val/10;
                               }
                               cout<<"Sum = "<<sum<<endl;
                     }
};

int main() { 
        sumofdigit obj;
        obj.calc_sumofdigit();
        return(0);
}

Output:


2^50 = 1125899906842624
Sum = 76

By +Dixit Singla 

Reverse Alphabet Codes


Given the following information:

a = 26, b = 25, c = 24, d = 23 ..... x = 3, y = 2, z = 1

What is the sum of each letter of this sentence: "The quick brown fox jumped over the cow"? 

Note: A white space has no value

Language C++: 

#include<iostream>
using namespace std;

class rev_alph {
 
            private:
                   int sum;
                   string str;
            public:
                   rev_alph() { 
                           sum = 0;
                           str = "The quick brown fox jumped over the cow";
                   }
                   void reversealpha() { 
                           int i;
                           str[0] = tolower(str[0]);
                           int len = str.length();
                           for (i = 0 ; i < len ; i++) { 
                                     if (str[i] == ' ') {
                                               sum += 0;
                                      }
                                      else {
                                               sum += (26 - (str[i] - 'a'));
                                      }
                            }
                            cout<<sum<<endl;
   
                     }
};

int main() {
       rev_alph obj;
       obj.reversealpha();
       return(0);
}

Output:
450

By +Dixit Singla 

A Power Function


Given the following function and it's results:

f(x,y) = x^y + y^x where x>0 and y>1f(1,2) = 1^2 + 2^1 = 3f(2,3) = 2^3 + 3^2 = 17f(3,4) = 3^4 + 4^3 = 145. . .

Using the above function denoted by f(x,y), and given that the initial values of x = 1 and y = 2, which increase by 1 each time, what are the last 4 digits of the sum of the first 15 function calls.

Language C++:

#include<iostream>
using namespace std;

class power_fun {

          private:
                 long long int sum,last_4_digit;
          public:
                 power_fun () { 
                          sum = 0;
                          last_4_digit = 0;
                 }
                 void show_last_digit() {
                           int x = 1,y = 2,i;
                           for(i = 0 ; i < 15 ; i++) {
                                     sum = sum + function_solver(x,y);
                                     x++;
                                     y++;
                            }
                            cout<<"sum = "<<sum<<endl;
                            last_4_digit = sum % 10000;
                            cout<<"last_4_digit = "<<last_4_digit<<endl;
                   }
                   long long int function_solver(int xx,int yy) { 
                            long long int t1 = 1,t2 = 1;
                            int i;
                            for (i = 1 ; i <= yy ; i++)
                                        t1 = t1*xx;
    
                            for (i = 1 ; i <= xx ; i++)
                                        t2 = t2*yy;
    
                            return(t1+t2);
                   }
};

int main() {
          power_fun obj;
          obj.show_last_digit();
          return(0);
}

Output:

sum = 7910956276398901049
last_4_digit = 1049

US Telephone Keypads


Given the following information about a US telephone touch tone keypad:

   1: (NONE)         2: A,B,C        3: D,E,F
     4: G,H,I              5: J,K,L         6: M,N,O
     7: P,R,S              8: T,U,V        9: W,X,Y

calculate the product of each characters value.

As an example, say the user enters: "Practice", the product would be:
7 * 7 * 2 * 2 * 8 * 4 * 2 * 3 = 37,632

What is the value of this string: "Programming Challenges are fun"?

Language C++:

#include<iostream>
#include<string.h>
using namespace std;

class uskeypad {
 
    private:
           string my_string;
           long long int prod;
    public:
  
           uskeypad() { 
                  prod = 1;
                  my_string = "Programming challenges are fun";
           }
           void calc_prod() {
   
                  int i;
                  int len = my_string.length();
                  for (i = 0 ; i < len ; i++) {
    
                            if(my_string[i] == ' ') {
                                      prod = prod * 1;
                            }
                            else if(
                                  my_string[i] == 'a' || 
                                  my_string[i] == 'b' || 
                                  my_string[i] == 'c' || 
                                  my_string[i] == 'A' || 
                                  my_string[i] == 'B' ||
                                  my_string[i] == 'C' 
                                  ) {
     
                                           prod = prod * 2;
                             }
                             else if(
                                  my_string[i] == 'd' || 
                                  my_string[i] == 'e' || 
                                  my_string[i] == 'f' || 
                                  my_string[i] == 'D' || 
                                  my_string[i] == 'E' ||
                                  my_string[i] == 'F' 
                                    ) {
                                           prod = prod * 3;
                             }
                             else if(
                                  my_string[i] == 'g' || 
                                  my_string[i] == 'h' || 
                                  my_string[i] == 'i' || 
                                  my_string[i] == 'G' || 
                                  my_string[i] == 'H' ||
                                  my_string[i] == 'I' 
                                    ) { 
                                           prod = prod * 4;
                             }
                             else if(
                                  my_string[i] == 'j' || 
                                  my_string[i] == 'k' || 
                                  my_string[i] == 'l' || 
                                  my_string[i] == 'J' || 
                                  my_string[i] == 'K' ||
                                  my_string[i] == 'L' 
                                   ) { 
                                           prod = prod * 5;
                             }
                             else if(
                                  my_string[i] == 'm' || 
                                  my_string[i] == 'n' || 
                                  my_string[i] == 'o' || 
                                  my_string[i] == 'M' || 
                                  my_string[i] == 'N' ||
                                  my_string[i] == 'O' 
                                   ) {
                                           prod = prod * 6;
                             }
                             else if(
                                  my_string[i] == 'p' || 
                                  my_string[i] == 'r' || 
                                  my_string[i] == 's' || 
                                  my_string[i] == 'P' || 
                                  my_string[i] == 'R' ||
                                  my_string[i] == 'S' 
                                   ) { 
                                           prod = prod * 7;
                             }
                             else if(
                                  my_string[i] == 't' || 
                                  my_string[i] == 'u' || 
                                  my_string[i] == 'v' || 
                                  my_string[i] == 'T' || 
                                  my_string[i] == 'U' ||
                                  my_string[i] == 'V' 
                                   ) {
                                           prod = prod * 8;
                             }
                             else if(
                                   my_string[i] == 'w' || 
                                   my_string[i] == 'x' || 
                                   my_string[i] == 'y' || 
                                   my_string[i] == 'W' || 
                                   my_string[i] == 'X' ||
                                   my_string[i] == 'Y' 
                                    ) { 
                                           prod = prod * 9;
                              }
                  }
                  cout<<"prod = "<<prod<<endl;
   
        }
};
int main() {

       uskeypad obj;
       obj.calc_prod();
       return(0); 
}

Output:
prod = 208129028102553600

By +Dixit Singla 

Pattern

Write a program to display the following pattern in C language (Pyramid in square)?

+ + + + + * + + + + +
+       *   *       +
+     *       *     +
+   *           *   +
+ *               * +
* * * * * * * * * * *



#include<stdio.h>
int main() {

int i,j;
int n = 11;        // Value of n should be ODD

for (i = 0 ; i <= (n / 2) ; i++) {
      for (j = 0 ; j < n ; j++) {
                if (i == 0) {
                     if (j == (n / 2 ))
                              printf ("* "); 
                     else
                               printf ("+ ");
                }
                else if (i == (n / 2)) {
                     printf ("* ");
                } 
                else {
                      if (j == 0 || j == (n - 1))
                               printf ("+ ");
                      else if (j == ((n / 2) - i) || j == ((n / 2) + i))
                               printf("* ");
                      else
                               printf ("  ");
                      }
                 } 
                 printf ("\n");
      }
} 

Output:-
+ + + + + * + + + + +
+       *   *       +
+     *       *     +
+   *           *   +
+ *               * +
* * * * * * * * * * *

Note:- To increase or decrease the size of the pattern change the value of variable "n" But keep in mind that the value of n should be ODD (1,3,5,7.....)

By +Dixit Singla 

Sum of factorials

Given the first few factorials:
1! = 1
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
What is the sum of the first 15 factorials, NOT INCLUDING 0!?

Language C++ :

#include<iostream>
using namespace std;

class fact {
 
          private:
                 unsigned long long int num,sum;
          public:
                 fact() {
                       num = 0;
                       sum = 0;
                 }
                 void fact1() {
                       int i;
                       for(i = 1 ; i <= 15 ; i++) {
                                 sum = sum + factorial_finder(i); 
                       }
                       cout<<"Sum of first 15 factorials is = "<<sum<<endl;
                  }
                  long long int factorial_finder(int val) { 
                                 unsigned long long int i,f = 1;
                                 for (i = 1 ; i <= val ; i++) {
                                               f = f*i;
                                 }
                                 return(f);
                   }
};

int main() {
        fact obj;
        obj.fact1();
        return(0);
}

Output: 

1401602636313

Unsigned long long long int is of 8 Bytes (64 Bits) 

Environment used to run this program :
  • Gcc compiler
  • Ubuntu 11.10 (Linux)
  • vim editor
By +Dixit Singla


Monday, June 10, 2013

Find Prime number in C

Program to find Prime number in C?


Prime Number:  Prime number is a natural number that can only be divided  by 1 or itself .

For example:
13 is a prime number because no number can divide 13 completely. 13 can only be divided by 1 or 13(itself).

Program in C:

#include<stdio.h>
int main()
{
 int i;
 int n;
 int count = 0;
 printf("enter the number \n ");
 scanf("%d",&n);
 
 for(i = 2 ; i < n ; i++)
 {
  if((n%i) == 0)
  { 
   count++;
   break;
  }
  
 }
 
 if(count == 0)
 {
  printf("%d is a prime number \n",n);
 }
 
 else
 {
  printf("%d is not a prime number \n",n);
 }
 
 return 0;                                          
}   

Output:
enter the number
12
12 is not a prime number

enter the number
13
13 is a prime number

By:  +Dixit Singla 

Wednesday, October 10, 2012

sizeof() Operator

sizeof() Operator :

sizeof operator is a unary operator.

Unary operator: Unary operation is an operation with only one operand that is which takes single input.

sizeof operator is used to know the exact size of any data type (In bytes).

sizeof (Data type);

Example_1 with code:
1:  #include<stdio.h>  
2:  void main(void)  
3:  {  
4:      int i;  
5:      float j;  
6:      char k;  
7:      printf ("int size = %d\n",sizeof(int));  
8:      printf("float size = %d\n",sizeof(float));  
9:      printf("char size = %d\n",sizeof(char));  
10:      printf("int size = %d\n",sizeof(i));  
11:      printf("float size = %d\n",sizeof(j));  
12:      printf("char size = %d\n",sizeof(k));  
13:  }  
Output:
1:  int size = 4  
2:  float size = 4  
3:  char size = 1  
4:  int size = 4  
5:  float size = 4  
6:  char size = 1  


Example_2 with code:

1:  #include<stdio.h>  
2:  struct a  
3:  {  
4:      int a;  
5:      float b;  
6:      int c;  
7:  };  
8:  void main(void)  
9:  {  
10:    struct a obj;   
11:    printf("struct a size = %d\n",sizeof(struct a));  
12:    printf("obj size = %d\n",sizeof(obj)); 
13:  }  
Output:
1:  obj size = 12  
2:  struct a size = 12  


From the output of example 1 we can clearly see that the size of int and size of float is 4 bytes and size of char is 1 byte.

 In example 2 we have declared a structure "a" having members

int a;
float b;
int c;

what would be the size of structure a?

From example one we observed that int and float is of 4 bytes so the size of structure is
4 + 4 + 4 = 12 bytes
so the output of second example of first "printf" is 12.
In the example we have declared a variable of type struct a
struct a obj;
obj has reserved 12 bytes of memory so when we will print the sizeof (obj) then it will also yield 12 bytes.

Stray Pointer

Stray pointer: 

Stray pointers are some times very dangerous because with stray pointer your program can crash. As we can guess from the name "stray" stray pointers are really stray having no home. Let me clear you with an example.

Suppose in your program you have dynamically allocated some memory (Using malloc( )) and then after using that memory you freed that memory (Using free( )).

Now stray pointer will be created if you will access the deleted memory or you will assign some value at that address.

Example with C program:

1: #include<stdio.h>  
2: void main(void)  
3:{  
4:  int i;  
5:  int *ptr;  
6:  ptr = (int *)malloc(sizeof(int) * 5); // Dynamically allocating 20 Bytes of memory  
7:  for (i = 0 ; i < 5 ; i++)  
8:  {  
9:      *(ptr+i) = i;            // Assing values  
10: }  
11: for (i = 0 ; i < 5 ; i++)  
12: {  
13:     printf("value = %d\n",*(ptr+i));  // printing values on console  
14: }  
15: free(ptr);               // Freeing memory   
16: for (i = 0 ; i < 5 ; i++)   
17: {  
18:     *(ptr+i) = i;            //uh oh, This was deleted!!  
19: }  
20:}  

Explanation: 

In the 6th line we have dynamically allocated 20 Bytes (int is of 4 bytes) of memory.

Now pointer "ptr" has the base address of allocated 20 bytes. 
From line 7 to 10 we are assigning values 
From line 11 to 14 we are printing assigned value at console 
In 15th line we freed the memory by calling free function 
From line 16 to 19 That's the main problem here we are assigning the value which is unknown. We are using that memory which does not belong to us.

Monday, October 1, 2012

Fibonacci Series using Recursion

Write a program to print Fibonacci series using recursion?

1:  #include <stdio.h>  
2:  void fibo(int,int,int);  
3:  void main(void)  
4:  {  
5:    int x,y,c = 10;  
6:    x = 0;  
7:    y = 1;  
8:    printf("1 ");  
9:    fibo(x,y,c);  
10:  }  
11:  void fibo(int x,int y,int c)  
12:  {  
13:    int z;  
14:    if (c < 1)  
15:      return;  
16:    else  
17:    {  
18:      z = x + y;  
19:      printf("%d ",z);  
20:      fibo(y,z,c - 1);  
21:     }  
22:  }  

Output:


 1 1 2 3 5 8 13 21 34 55 89  



Sunday, September 30, 2012

Pointer's

You already know that pointer is most difficult topic in c and c++ languages here i will tell you what is pointer and how you can use pointer with arrays,functions and structures etc.

What is pointer :- Pointer is the user defined data type which can hold the address of simple data type like int,char,float and user define data type   like function,pointer etc. and derived data type like array,structure and union. pointer is denoted by *   eg :- int *x;

Before pointer we will discus what are simple variables like int,float and char etc. how simple variables are stored in computer memory.

Example :- int x =10;

                                                                   x
           10          
                                                                   0x5278

Suppose above box is block of computer memory.
Above fig. is showing that how 10 is stored in memory.
What is x?
What is 10?
What is 0x5278?
0x5278 is the address where 10 is stored and x is the name of address (or location) and 10 is the value of x or value at that address.

Pointer declaration  :- How pointer is declared ?
1. int *iptr;
2. char *cptr;
3. float *fptr;
Here iptr is a pointer variable which can hold the address of int type similarly cptr can hold the address of char type and fptr can hold float type.

How pointer is used :-
int x = 20;
int *iptr;
iptr = &x;

--In first statement simple variable is declared.
--In second statement pointer variable is declared.
--In third statement address of value 10 is assigned to iptr means now value of iptr is the address of the value 10 that is 0x5278.
                                                               
                                                                  x

           20            
                                                                  0x5278

                                                                  iptr
        0x5278       
                                                                  0x3678

0x3678 is the address where address of x is stored having name iptr.
Now how we will access the value of x by using iptr?
printf("%d\n",*iptr);
above statement will print the value of x means the output will be 10
Note :- int type pointer will store the address of only int type variable you can't store the address of float,char into int type pointer......
int x = 10;
float *fptr;
fptr = &x;
Third statement is wrong because x is an int type and fptr is a float type and you can't assign the address of int to float type pointer .
But if you want to do this then you will first typecast it.
Important fact about pointer :- 
                                    *&p = p;
 But &*p = p will not compile.
----------------------------------------------------------------------------------------------------------------------------------
Pointer with arrays :-

Array :- Array is a derived data type it is used  to store the homogeneous values and the main fact is arrays are used to allocate the contiguous memory.
Example :- int arr[10] = {10,20,30,40,50,60,70,80,90,100};
above statement will reserve the 20 bytes of contiguous memory. because int is of 2 bytes in 16 bit compiler.
array structure :-
assume the base address is 0x5270

                    arr         
   10       20       30       40      50      60      70      80      90      100   
                    0x5270
What is arr?
What is 0x5270?
What is 10,20,30------100;
0x5270 is the base address of the whole array and arr is the name given to the base address and 10,20-----100 are values of the arr. By using base address you can access the whole values of the arr 
if you will print the arr then it will print the base address
Example :-  printf("%p\n",arr);
output :- 0x5270
1. arr;
2. &arr;
3. arr[0];
Above three statements will give the same value that is the base address 0x5270.
Note :- you can't change the base address. You can't  perform addition, subtraction, multiplication and division with base address.
Example :- arr = arr + 1;
                   arr = arr - 1;
                   arr = arr * 2;
                   arr = arr / 5;
Above all statements are wrong because if you will change the base address then there is no way to access the previous values(elements) of array.

Note :- Array is a constant pointer You can't change the array's base address.

Access the whole array elements using a pointer variable :- 
Example:- 
Int *ptr;
Int arr[10] = {10,20,30,40,50,60,70,80,90,100};
ptr = &array[0];            //  assign base address of "arr" to "ptr"

Assume base address is 0x5270


                    arr
   10       20       30       40      50      60      70      80      90      100   
                    0x5270


                                                                    ptr
     0x5270     
                                                                    0x6488


Above Fig. shows how pointer variable "ptr" holds base address of an array "arr"
Now you can access the all elements of "arr" using pointer variable "ptr"  like
1. printf("%d",*(ptr+i));   or        //  "*" means "value at address"
2. printf("%d",ptr[i]);
Above both statements will print the all elements of array "arr"

----------------------------------------------------------------------------------------------------------------------------------
Pointer with functions :- 

Function :- Function is build of three things.
1. Function declaration.
2. Function call.
3. Function definition.
Example :- 
// parameterized function with return value.
#include<stdio.h>
int sum(int,int);           // Function declaration
void main()
{
         int x = 10;
         int y = 20;
         int c;
         c = sum(x,y);        //Function call
         printf("%d\n",c);
         return 0;
}
int sum(int a,int b)    // Function definition
{
          int z;
          z = a+b;
          return(z);      // Return statement
}
Output :- 30

Pointer with function :-
Using pointer we can pass the address of the variable
Example :-

#include<stdio.h>
int sum(int *,int *);       // pointer type parameters
void main()
{
         int x = 10;
         int y = 20;
         int c;
         c = sum(&x,&y);  // passing address of x and y
         printf("%d",c));
         return 0;
}
int sum(int *a,int *b)   // Now address of x and y is in a and b
{
          int z;
          z = (*a)+(*b);      //Adding "value at a" and "value at b".
          return(z);             //Returning sum of "value at a" and "value at b"
}

Note :- Only using pointer you can return more than one values.


Thursday, September 6, 2012

Pattern

Program to output below pattern?


1:  #include <stdio.h>  
2:  void main(void)  
3:   {  
4:       int i,j;  
5:       for(i = 1 ; i < 5 ; i++)   
6:       {  
7:            for(j = 1 ; j <= i ; j++)  
8:            {  
9:                  if((j % 2) == 0)   
10:                        printf("1 ");  
11:                  else  
12:                        printf("0 ");  
13:            }  
14:            printf("\n");  
15:       }  
16:  }   

Output:

 

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