Introduction To Programming

Why programs ? Programming is nothing but making the computers think the same way a human thinks. Programs are made for obtaining a specific solution which can even be solved by humans which when solved by us may consume time..

Saturday, January 23, 2010

#include< stdio.h >
main()
{
int a=1,b;
b=a++*2*--a;
printf("%d %d",a,b);
}


Output:

1 0

How it works?
refer precedence operator table
pre decrement(--) will decrement value of 'a'. now a=0.
a++ is post increment, so a will be incremented after multiplication. so
b=0*2*0=0
but after this a will incremented and a=1(because of post increment).

Lparts

#include< stdio.h >
main()
{
int a=1,b;
b=a++*2;
printf("%d %d",a,b);
}




Output:

2 2

How it works?
we use post increment so increment operation of 'a' will be done after multiplication . so b=2. then a value will be incremented so a=2

Find odd or even without Relational operators

#include< stdio.h >
main()
{
int a;
scanf("%d",&a);
if(a&1)
printf("odd");
else
printf("even");
}

How it works?
'&' logical 'and' operator, this will 'and' a with 1.
all odd numbers have last bit value '1'. for example








Odd
1 have 0001
3 have 0011
5 have 0101








Even
2 have 0010
4 have 0100
6 have 0110


from above table we can come to one decision i.e., odd numbers will have last bit value as '1'. so we'and' the number with '1'. if result is 1, then 'if' will work and print "odd". if result is 0, then 'else' will work and print "even"