Monday, 12 September 2011

Shows logical operator

Q.2 Write a program in c for showing working of different logical operator in C.
Your program should guide user with proper message/menu on the console.
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("\nPlease Press Your Choice");
 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(); 
    printf("\nEnter your age : "); 
    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

Please Press Your Choice
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) :


Enter your age : 31 


Person is insured!!

Logical NOT operator

No comments:

Post a Comment