Tuesday, 19 April 2011

Example program on if..else condition

/*    Two-way selection.
      */
#
include < stdio.h >

 int main(void) {
  // Local Declarations
  int a;
  int b;

  // Statements
  printf("Please enter two integers: ");
  scanf("%d%d", & a, & b);

  if (a <= b) {
   printf("%d <= %d\n", a, b);
  } else {
   printf("%d > %d\n", a, b);
  } //end a <= b if

  return 0;
 } // main

/*    Results:
Please enter two integers: 10 15
10 <= 15
*/



 Conditional statements
 



character handling functions: Example program on character handling functions

/*    This program demonstrates the use of the character
    classification functions found in the c-type library.
    Given a character, it displays the highest
    classification for the character.
*/

#include < stdio.h >
#include < ctype.h >

 int main(void) {
  //    Local Declarations
  char charIn;

  //    Statements
  printf("Enter a character to be examined: ");
  scanf("%c", & charIn);

  if (islower(charIn))
   printf("You entered a lowercase letter.\n");
  else if (isupper(charIn))
   printf("You entered an uppercase character.\n");
  else if (isdigit(charIn))
   printf("You entered a digit.\n");
  else if (ispunct(charIn))
   printf("You entered a punctuation character.\n");
  else if (isspace(charIn))
   printf("You entered a whitespace character.\n");
  else
   printf("You entered a control character\n");
  return 0;
 } // main

/*    Results:
First Run
    Enter a character to be examined: a
    You entered a lowercase letter.
Second Run
    Enter a character to be examined: ~
    You entered a punctuation character.
*/