9.12.2011

Shows logical operator

Q.2 Write a program in c for showing working of different logical operator in C.
Ans.

/*c program to shows for working of logical operator*/
#include<stdio.h>
#include<conio.h>
int main()
{
int age,ch;
char ms; //marital status
printf("\nPress 1 for Logical AND operator");
printf("\nPress 2 for Logical OR operator");
printf("\nPress 3 for Logical NOT operator :");
scanf("%d",&ch);
switch(ch)
{
case 1 :
printf("\n\nYou press 1 for logical AND operator\n");
printf("\nA person is insured if he is married and his age>=30\n");
fflush(stdin);
printf("\nPerson married or unmarried Enter y/n : ");
ms=getchar();
printf("\nEnter person age : ");
scanf("%d",&age);
if((ms=='y' || ms=='Y')&& age>=30)
printf("\nPerson is insured!!");
else
printf("\nPerson is not insured!!");
break;

case 2 :
printf("\nYou press 2 for logical OR operator\n");
printf("\nA person is insured either he is married or his age>=30\n");
fflush(stdin);
printf("\nPerson married or unmarried Enter y/n : ");
ms=getchar();
scanf("%d",&age);
if((ms=='y' || ms=='Y')|| age>=30)
printf("\nPerson is insured!!");
else
printf("\nPerson is not insured!!");
break;

case 3 :
printf("\nYou press 3 for logical NOT operator\n");
printf("\nA person is insured if he is married\n");
fflush(stdin);
printf("\nPerson married or unmarried Enter y/n : ");
ms=getchar();
if(!(ms=='y' || ms=='Y'))
printf("\nPerson is insured");
else
printf("\nPerson is not insured");
break;

default :
printf("\nYou press wrong number!");
}
getch();
return 0;
}

Output of the above program

Press 1 for Logical AND operator
Press 2 for Logical OR operator
Press 3 for Logical NOT operator:  1

You press 1 for logical AND operator

A person is insured if he is married and his age>=30

Enter y/n (y=married and n=unmarried) :

Person is insured!!

 Logical NOT operator

1. wow it's working

2. #include
#include
main()
{
inta,b;
clrscr();
printf("enter the values of a,b);
scanf("%d,%d",&a,&b);
printf("and of a and b is :%d",a&&b);
printf("or of a and b is :%d",a||b);
getch();
}