Computer Concept & Programming In C - Unit III - 4

printf("\nEnter 1 if married or 0 if unmarried\n");
scanf("%d",&marital_status);
printf("\nEnter the age\n");
scanf("%d",&age);
printf("\nEnter 1 for male and 0 for female\n");
scanf("%d",&sex);

/* checking conditions for the
   insurance of the driver     */

/* case 1*/
if(marital_status == 1)
 {
  printf("\n You will be insured");
  flag = 1;
 }
else
  {
    /* case 2 */
    if(sex == 1)
      if(age > 30)
       {
printf("\n You will be insured");
flag = 1;
       }
    /* case 3 */
    if(age < 30)
      if(age > 25)
{
 printf("\n You will be insured");
 flag = 1;
}
  }
  if(flag == 0)
   printf("\n You will not be insured.");
 printf("\nPress any key to exit");
 getch();
 }

The if-else-if ladder: -
The condition and their associated statements can be arranged in a construct that takes the form:
if (condition 1)
Statement 1;
else if (condition 2)
Statement 2;
else if (condition 3)
Statement 3;
else
Statement n;
The different conditions are evaluated from the top down and whenever a condition is evaluated as TRUE, the corresponding statement(s) are executed and the rest of the construct is skipped. This construct is referred to as the if-else-if ladder. To demonstrate the use of the ladder consider this example. Suppose that you want to test the ASCII code of a received character to check if it is an alphabetic character. If it is so, you would like to check if it is lowercase or uppercase. Knowing that the ASCII codes for the uppercase letters are in the range 65-90, and those for the lowercase letters are in the range 97-122, you may use logic as follows:
1. If the code is greater than 64 and less than 91 then the character is an uppercase letter.
2. If the code is greater than 96 and less than 123 then the character is a lower case letter.
3. Other than that, the code does not represent an alphabetic character. The following program puts the logic into action:
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
void main ( )
{
char a;
printf (“\ n Enter an alphabetic character:”);
a = getch ( );
if ((a > 64) && (a < 91))
printf (“\ n The character is an uppercase letter”);
else if ((a > 96) && (a < 123))
printf (“\ n The character is a lowercase letter”);
else
printf (“\n This is not an alphabetic character:”);
}
The following are sample runs for the program:
Enter an alphabetic character: a
The character is a lowercase letter.
Enter an alphabetic character: Z
The character is an uppercase letter.
Enter an alphabetic character: [
This is not an alphabetic character.