if (marks >= 0 && marks <= 100)
printf(“Valid marks”);
else printf(“Invalid marks”);
This program segment uses an if-else statement to test whether the marks entered are valid or not and print the appropriate message. It is assumed that for marks to be valid, it should be in the range 0 to 100, i.e., greater than or equal to zero and less than or equal to 100. We may rewrite the above code by using a test for invalid marks. The value of marks is invalid if it is outside the range 0 to 100, i. e., less than 0 or greater than 100. The if-else statement using such a test is given below.
if (marks < 0 || marks > 100)
printf(“Invalid marks”);
else printf(“Valid marks”);
Note that there is also another way to express the validity of marks: it should not be invalid, i. e., it should not be outside the range 0 to 100. This logic is used in the if statement given below.
if (! (marks < 0 || marks > 100))
printf(“Valid marks”);
else printf(“Invalid marks”);
Example: Validity of marks in a single subject
#include <stdio.h>
void main()
{
int marks;
clrscr();
printf(“Enter the Marks :\n”);
scanf(“%d”,&marks);
if (marks >= 0 && marks <= 100)
printf(“Valid marks”);
else printf(“Invalid marks”);
getch();
}