II-60 Programming Concepts
Examples on logical operators
9. Program on simple logical problem
void main()
{
int a=4,b=2,m,n,o;
clrscr();
m=a&&b;
n=a||b;
o=a==b;
printf(" %d",m);
printf(" %d",n);
printf(" %d",o);
getche();
}
OUTPUT:
1
1
0
Examples on relational and logical operators
10. Program on simple logical problem
void main()
{
int x=2,y=0,z=8,p;
clrscr();
z=x>y&&y;
p=x||z;
printf(" %d %d",z,p);
getch();
}
OUTPUT:
0
1
M03_ITL-ESL4791_02_SE_C03.indd 60 12/22/2012 5:00:08 PM
Operators and Expressions II-61
11. Program on logical problem
void main()
{
int x=2000,y=1000,z=20,p;
clrscr();
p=(x>z && y<z);
printf(" %d",p);
getch();
}
OUTPUT:
0
12. Program on logical operation
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf(" Condition : Return Values " );
printf(" 5>3 && 5<10 : %5d",5>3 && 5<10);
printf(" 8>5 || 8<2 : %5d",8>5 || 8<2);
printf(" !(8==8) : %5d",!(8==8));
}
OUTPUT:
Condition : Return Values
5>3 && 5<10 : 1
8>5 || 8<2 : 1
!(8==8) : 0
Examples are given on (+) unary operator
13. Program on unary operator
void main()
{
float a=+15.4,c=-4.6;
clrscr();
c+=a;
M03_ITL-ESL4791_02_SE_C03.indd 61 12/22/2012 5:00:08 PM
II-62 Programming Concepts
printf("%10.2f",c);
getche();
}
OUTPUT:
10.80
14. Program on unary operator
void main()
{
int x=40,y=50,z=10;
clrscr();
x+=y;
z+=x;
printf("%d %d ",x,z );
getch();
}
OUTPUT:
90
100
Examples on sizeof() and & operators
15. Find the size of bytes for integer, character and long integers.
void main()
{
clrscr();
printf(" size of int =%d",sizeof(int));
printf(" size of char =%d",sizeof(char));
printf(" size of long =%d",sizeof(long));
getche();
}
OUTPUT:
size of int = 2
size of char = 1
size of long = 4
M03_ITL-ESL4791_02_SE_C03.indd 62 12/22/2012 5:00:08 PM
Operators and Expressions II-63
16. Find the size of bytes for integer, character and long integers.
#include <stdio.h>
#include <conio.h>
void main()
{
char x='a';
int y=2;
long int z=23456;
float p=2.345;
double q=12e10;
clrscr();
printf(" size of a character(x)=%d byte",sizeof(x));
printf(" size of an integer(y)=%d bytes",sizeof(y));
printf(" size of long integer(z)=%d bytes",sizeof(z));
printf(" size of float(p)=%d bytes",sizeof(p));
printf(" size of double float (q)=%d bytes",sizeof(q));
printf(" Address of x=%u y=%u z=%u p=%u q=%u",&x,&y,&z,&p,&q);
getche();
}
OUTPUT:
size of a character(x)= 1 byte
size of an integer(y)=2 bytes
size of long integer(z)= 4 bytes
size of float(p)= 4 bytes
size of double float (q)= 8 bytes
Address of x = 65479 y = 65480 z = 65482 p = 65486 q = 65490
Examples on bitwise operators.
17. Left and right shift
void main()
{
int x=2,y=16,z;
clrscr();
x<<=2,y>>=1;
x&=y;
z=y|x;
printf("%d %d ",x,z);
getche();
}
OUTPUT:
8 8
M03_ITL-ESL4791_02_SE_C03.indd 63 12/22/2012 5:00:08 PM
II-64 Programming Concepts
18. Complement and ORing operation
void main()
{
int p=2,x=8,y=5,z;
clrscr();
x=~x;
z=y|p;
printf("%d %d",x,z);
getche();
}
OUTPUT:
-9 7
Find the bug/s in the following program/s.
Arithmetic operators
1.
void main()
{
int x=2,y=2,z=4;
clrscr();
z=((x+y)*z/2);
printf("%f",z);
}
Bug: %f is not expected in printf(). With %d the answer would be 8
2.
void main()
{
int x=10,y=20,z=30;
clrscr();
z=((x+y)*z/2);
z=z%4;
print("%d",z);
}
Bug: printf() is needed instead of print
Answer with correction would be 4
M03_ITL-ESL4791_02_SE_C03.indd 64 12/22/2012 5:00:09 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset