Thursday, January 2, 2014

Constants

Constants: the items whose value do not change during program execution.
The constants include Integer, Real and Character constants.




Constant type
Permissible Range
Integer Constant
-2147483648 (231) to 2147483647 (231-1)
Real Constant
-3.4*10­­38­ to 3.4*1038­
Character Constant
Can have maximum length of 1






 Rules for constructing Integer Constants
  1. Should have at least one digit.
  2. It must not have a decimal point.
  3. Can be either positive or negative.
  4. If no sign precedes a number, it is taken positive.
  5. No commas or blanks are allowed within the Integer Constant.

Valid Integer Constants
Invalid
4284
4,284
-38482
4 285
+42873
792.654



Rules for constructing Real Constants
  1. Should have at least one digit. 
  2. Must have a decimal point.
  3. Can be either positive or negative.
  4. Default sign is positive.
  5. No commas or blanks allowed.
  6. Big real constants can be expressed in exponential form.
  7. The mantissa and exponential part is separated by letter e or E.
  8. Default sign for mantissa and exponent sign is positive. 

0.000738 = 7.38 * 10-4
Here 7.38 is the Mantissa part
And 10-4 is the Exponent part
In exponential form it can be written as
7.38e-4 Or 7.38E-4

Valid Real Constants
Invalid
8912.12
4284
0.1231
4 285 . 06
-2371.001
Five.seven
-0.25
1,000.7
+3.2e5
4.3e
-4.32e-7
2E
3.55E-5
4,009E
-234.01E+6
2 e 21




Rules for constructing Character constant

1.      Must be enclosed without single quotation marks.
2.      Must be of maximum length 1.
3.      Can be a alphabet, a number or a special symbol.
 
Examples : 'A',  '9',  '=' 




Wednesday, January 1, 2014

To find whether a number is in power of 2 using complement and gate (very short)

A number occurring in power of has a specialty when written in its binary representation:

1     = 0000 0001
2     = 0000 0010
4     = 0000 0100
8     = 0000 1000
16   = 0001 0000
32   = 0010 0000
64   = 0100 0000
128 = 1000 0000

What happens when I subtract one from these above numbers

1-1=0          0000 0000
2-1=1          0000 0001
4-1=3          0000 0011
8-1=7          0000 0111
16-1=15      0000 1111
32-1=31      0001 1111
64-1=63      0011 1111
128-1=127   0111 1111

Before moving further we will look at Complement Operator(~) in C.
Complement of a binary number changes all 1 bits to 0 and all zero bits to 1

~(1011 0110) = 0100 1001

It is similar to XOR toggling all bits
          1011 0110
XOR    1111 1111
------------------------
           0100 1001

Never mind!

Looking at numbers in power of two and their subtraction by 1, we can establish a pattern

For example, let us take 16
 16 =  0001 0000
 ~(16) = ~(0001 0000) = 1110 1111
16-1=15 = 0000 1111

If we AND  ~16 and (16-1), it equals (16-1)

~(16) =         1110 1111
16-1=15=      0000 1111
----------------------------------
 16-1=15=      0000 1111

Hence the method to find whether the number is power of two or not.

#include<stdio.h>
int main()
{
    int num;
    printf("\nEnter number:");
    scanf("%d",&num);
    if(((num-1)&(~num))==(num-1))
        printf("%d is in power of 2",num);
    else
        printf("%d is not in power of 2",num);

    return 0;
}

Although, this short method does not tell us which power of 2 is the number like previous methods discussed. You can verify this method for numbers that are not in power of two.






To find whether a number is in power of 2 by counting number of 1 bits


20=1 (or 2^0=1) Read as 2 raised to power 0
21=2 (or 2^1=2) Read as 2 raised to power 1
22=4 (2*2=4)
23=8 (2*2*2=8)
24=16
25=32
As previously explained, 32 is 2 raise to power 5 and 22 does not come in power of 2.

So is there anything special in these number. 
Yes, there binary representation is intuitively special.

1     = 0000 0001
2     = 0000 0010
4     = 0000 0100
8     = 0000 1000
16   = 0001 0000
32   = 0010 0000
64   = 0100 0000
128 = 1000 0000

Any number which exist in power of 2 has only single 1 in its representation.

So, we can convert a number to binary and if it has only a single 1, it is in power of 2.And write a program that does not use pow() function of math.h header file

 The conversion to binary form is already explained in earlier blog

#include<stdio.h>
int main()
{
    int num, num_copy, rem, count,power;
        printf("\nEnter number:");
        scanf("%d",&num);

    num_copy=num;
    count=0;
        power=-1;

        while(num>0)
        {
        rem=num%2;

        if(rem==1)
            count++;

        num=num/2;
        power++;
    }

        if(count==1)
        printf("%d is 2 raised to %d" ,num_copy,power);
    else
        printf("number not in power of 2");


    return 0;

}
      
        
        








To find whether a number is in power of 2 using pow()

A number is in power of 2, when 2 raised to some power is equal to that number.

Is 32 in power of 2? yes
2 raised to power 5 equals 32.

Is 256 in power of 2? yes
2 raised to power 8 equal 256

Is 24 in power of 2? no

In C, header file math.h provide us pow() function
pow(x,y) -- signifies x raised to power y

So we can easily check if number is in power of two
Steps
1.Enter number
2.Set flag=0
3.Set check=0
4.Set i=0
5.loop until check < number
   1.check = pow(2,i) [ 2^i]
   2. if check equals number then Set flag equal to 1 and break from loop goto step 6
   3. increment i = i+1
6.if flag equals to 1 then number is 2 raised to power i
   else it is not.

For example
number=4
flag=check=i=0

check< 4 [ 0<4  yes. Go inside loop]
check = pow(2,0) [2^0=1]
check == number [no]
i=i+1 [0+1=1]

check< 4 [ 1<4  yes Go inside loop]
check = pow(2,1) [2^1=2]
check == number [no]
i=i+1 [1+1=2]

check< 4 [ 2<4  yes Go inside loop]
check = pow(2,2) [2^2=4]
check == number [yes] set flag=1, break from loop-goto step 6

is flag == 1 [yes]
print(number is 2 raised to power 2( value of i=2 when we break from loop)


#include<stdio.h>
#include<math.h>

int main()
{

    int i, num,check, flag;
    printf("\nEnter number:");
    scanf("%d",&num);

    i=0;
    check=0;
    flag=0;

    while(check<num)
    {
        check=pow(2,i);
        if(check==num)
        {
            flag++;
            break;
        }
        i++;
    }
    if(flag==1)
        printf("%d is 2 raised to power %d",num,i);
    else
        printf("Number is not in power of 2 ");

    return 0;
}












Toggle a bit.

Toggling a bit will mean changing a bit to 0 if it was originally 1 or changing a bit to 1 it was originally 0.

Suppose you have been given a bit pattern and you want to toggle all the bit in the pattern. How can you achieve it.

Simple XOR it with 1's equal to number of bits given

For example, toggle (1101 0101 01)--> 0010 1010 10

           1101 0101 01
XOR     1111 1111  11   
---------------------------
           0010 1010 10

Toggling the Nth bit of a bit pattern can be achieved by left shifting 1  (N-1) times and XORing it with the bit pattern

For example, change the toogle the 4th bit (from left) in 1010 1000
N=4
N-1=3

i=(1<<3);
1010 1000 XOR i

1= 0000 0001
i=1<<4 = 0000 1000

          1010 1000
XOR    0000 1000
-----------------------
           1010 0000

Simply
get n;
after_toggle = given_bit_pattern ^ (1<<(n-1));





Factorial

Factorial of a number 'n', denoted by n!, is the product of non negative number less than or equal to the given number 'n'.
n! = n*(n-1)!
n! = n*(n-1)*(n-2)*.....*(n-n)!
n! = n*(n-1)*(n-2)*.....*(0!)
n! = 0?
Nop
0! = 1
n! = n*(n-1)*(n-2)*....*(n-(n-1))*1
n! = n*(n-1)*(n-2)*.....*1*1
For example n=5
5! = 5 * 4 * 3 * 2 * 1* 0! 
5! = 5 * 4 * 3 * 2* 1 * 1
5! = 120


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

    int f, i, n ;

    printf("\nFactorial");
    printf("\nEnter the number(>=0):");
    scanf("%d",&n);

    if(n<0)
    {
        printf("Input not suitable");
        return -1;
    }

    f=1;
    for(i=2;i<=n;i++)
        f*=i;

    printf("%d! is %d",n,f);
    return 0;
}

Fibonacci Series

Fibonacci series is a series where f(n-1), f(n),f(n+1) are three terms in fibonacci series, then
f(n+1)=f(n-1)+f(n)
Each terms in fibonacci series is sum of previous two terms taking f(1)=1 and f(2)=1
The Series generated is as follow
1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ...............

Suppose we want to print fibonacci series upto  'n' numbers.
We know two intial terms of the series which are f(1)=1 and f(2)=1. Print these on the screen. take count =0 which will make the program loop till it is equal to 'n-2'. [first two terms are printed outside loop. So number of terms entered is subtracted by 2. n=n-2]
a=1, b=1 ,c=0,count=0
print(a)
print(b)
repeat until count is less than  'n-2'
 1. c = a + b
 2. print (c)
 3.  a = b    
 4.  b = c     
 5. count = count +1


for example n=5
a=1,b=1
n=n-2
count=0
print(a), print(b)

count<3 [0<3   yes]
c = a + b [a = 1, b =1,c = 1 + 1 = 2]
print (c)  [ prints 2]
a = b  [a=1]
b = c  [b=2]
count = count+1 [count = 0 + 1 =1]

count<3 [1<3 yes]
c = a + b [a = 1, b =2,c = 1 + 2 = 3]
print (c)   [ prints 3]
a = b  [a=2]
b = c  [b=3]
count = count+1 [count = 1 + 1 = 2]

count<3 [2<3 yes]
c = a + b [a = 2, b =3,c = 2 + 3 = 5]
print (c)   [ prints 5]
a = b  [a=3]
b = c  [b=5]
count = count+1 [count = 2 + 1 =3]

count<3 [ 3<3 No. Loop stops]

for n= 5, series containing 5 terms is printed
1 1 2 3 5

#include<stdio.h>
int main()
{
    int a, b, c, n, count;
    printf("\nFibonacci Series");
    printf("\nEnter number of terms(number>2):");
    scanf("%d",&n);

    a=1;
    b=1;
    printf("%d %d ",a,b);
        count=0;
    n=n-2;

    while(count<n)
    {
        c=a+b;
        printf("%d ",c);
        a=b;
        b=c;
        count++;
    }
    return 0;
}