Friday, 2 November 2012

Logical Operators


C allows usage of three logical operators, namely, &&, || and !. These are to be read as
  ‘AND’ ‘OR’ and ‘NOT’ respectively.

 
Don’t use the single symbol | and &. These single symbols also have a meaning. They are bitwise operators
example question:
Example 2.4 : The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules:
Percentage above or equal to 60   - First division
Percentage between 50 and 59   - Second division
Percentage between 40 and 49   - Third division
Percentage less than 40   - Fail
so the coding are as follow:
#include <stdio.h>
int main ( )
{
    int marks,status;
    printf("\nEnter marks = ");
    scanf("%d",&marks);
   
    if (marks > 79 && marks < 100)
    printf("\nA,Pass\n");
    else if(marks > 60 && marks < 79)
    printf("\nB,Pass\n");
    else if(marks > 40 && marks < 59)
    printf("\nC,Pass\n");
    else
    printf("\nF,Fail\n");
    scanf("%d",&marks);
return 0;
}
 

No comments:

Post a Comment