Tuesday, 14 January 2014

Explain Logical operator with example.

It check logical relation between two expressions.If relation is true it returns 1 otherwise 0.
The operators are logical AND(&&),logical OR(||),logical NOT(!=).

Write a program to print logic 1 if input character is capital otherwise 0.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a character ");
scanf(" %c ",&x);
y=( x>=65 && x<=90 ? 1:0 );
printf(" Y : %d ",y );


}

Output
Enter a character : A
Y : 1
Enter a character : a
Y : 0

Write a program to display 1 if inputted number is between 1-100 otherwise 0.Use logical AND(&&) operator.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a number:");
scanf(" %c ",&x);
y=( x>=1 && x<=100 ? 1:0 );
printf(" Y : %d ",y );
}

output
Enter a number:5
Y= 1


Write a program to display 1 if inputted number is either 1  or 100 otherwise 0.Use logical OR(||) operator.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a number:");
scanf(" %c ",&x);
y=( x==1 || x==100 ? 1:0 );
printf(" Y : %d ",y );
}

output
Enter a number:5
Y=0
Enter a number:100
Y=1

Write a program to display 1 if inputted number is between 1-100 otherwise 0.Use logical AND(&&) operator.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a number");
scanf(" %c ",&x);
y=( x>=1 && x<=100 ? 1:0 );
printf(" Y : %d ",y );
}

output
Enter a number:5
Y= 1

Write a program to display 1 if inputted number is not 100 otherwise 0.Use logical NOT(!=) operator.

#include<stdio.h>
#include<conio.h>
main()
{
char x;
int y;
clrscr();
printf(" \n Enter a number");
scanf(" %c ",&x);
y=( x!=100 ? 1:0 );
printf(" Y : %d ",y );
}

output
Enter a number:100
Y= 0

No comments:

Post a Comment