Part 9 Special case
#include <stdio.h> main() { float sum ; int n ; sum = 0 ; for( n = 1 ; n <= 10 ; ++n ) { sum = sum + 1/(float)n ; printf("%2d %6.4fn", n, sum) ; } } PROGRAM SHOWING THE USE OF A CAST
The program in shows the use of getchar function in an interactive environment. The program displays a question of YES/NO type to the user and reads the user's response in a single character (Y or N). If the response is Y, it outputs the message My name is BUSY BEE otherwise, outputs. You are good for nothing
#include<stdio.h> main() { char answer; printf("Would you like to know my name?n"); printf("Type Y for YES and N for NO: "); answer = getchar(); /* .... Reading a character...*/ if(answer == 'Y' || answer == 'y') printf("nnMy name is BUSY BEEn"); else printf("nnYou are good for nothingn"); }
The program of requests the user to enter a character and displays a message on the screen telling the user whether the character is an alphabet or digit, or any other special character This program receives a character from the keyboard and tests whether it is a letter or digit and prints out a message accordingly. These tests are done with the help of the following functions: isalpha(character) isdigit(character) For example, isalpha assumes a value non-zero (TRUE) if the argument character contains an alphabet; otherwise it assumes 0 (FALSE). Similar is the case with the function isdigit
#include <stdio.h> #include <ctype.h> main() { char character; printf("Press any keyn"); character = getchar(); if (isalpha(character) > 0) printf("The character is a letter."); else if (isdigit (character) > 0) printf("The character is a digit."); else printf("The character is not alphanumeric."); }
The program uses three new functions: islower, toupper, and tolower. The function islower is #include <stdio.h> #include <ctype.h> main() { char alphabet; printf("Enter an alphabet"); putchar('n'); /* move to next line */ alphabet = getchar(); if (islower(alphabet)) putchar(toupper(alphabet)); else putchar(tolower(alphabet)); }
#include <stdio.h> #include <ctype.h> main() { char alphabet; printf("Enter an alphabet"); putchar('n'); /* move to next line */ alphabet = getchar(); if (isupper(alphabet)) putchar(tolower(alphabet)); else putchar(toupper(alphabet)); }

C Programming Language Part 9

  • 1.
  • 2.
    #include <stdio.h> main() { float sum; int n ; sum = 0 ; for( n = 1 ; n <= 10 ; ++n ) { sum = sum + 1/(float)n ; printf("%2d %6.4fn", n, sum) ; } } PROGRAM SHOWING THE USE OF A CAST
  • 3.
    The program inshows the use of getchar function in an interactive environment. The program displays a question of YES/NO type to the user and reads the user's response in a single character (Y or N). If the response is Y, it outputs the message My name is BUSY BEE otherwise, outputs. You are good for nothing
  • 4.
    #include<stdio.h> main() { char answer; printf("Would youlike to know my name?n"); printf("Type Y for YES and N for NO: "); answer = getchar(); /* .... Reading a character...*/ if(answer == 'Y' || answer == 'y') printf("nnMy name is BUSY BEEn"); else printf("nnYou are good for nothingn"); }
  • 5.
    The program ofrequests the user to enter a character and displays a message on the screen telling the user whether the character is an alphabet or digit, or any other special character This program receives a character from the keyboard and tests whether it is a letter or digit and prints out a message accordingly. These tests are done with the help of the following functions: isalpha(character) isdigit(character) For example, isalpha assumes a value non-zero (TRUE) if the argument character contains an alphabet; otherwise it assumes 0 (FALSE). Similar is the case with the function isdigit
  • 6.
    #include <stdio.h> #include <ctype.h> main() { charcharacter; printf("Press any keyn"); character = getchar(); if (isalpha(character) > 0) printf("The character is a letter."); else if (isdigit (character) > 0) printf("The character is a digit."); else printf("The character is not alphanumeric."); }
  • 7.
    The program usesthree new functions: islower, toupper, and tolower. The function islower is #include <stdio.h> #include <ctype.h> main() { char alphabet; printf("Enter an alphabet"); putchar('n'); /* move to next line */ alphabet = getchar(); if (islower(alphabet)) putchar(toupper(alphabet)); else putchar(tolower(alphabet)); }
  • 8.
    #include <stdio.h> #include <ctype.h> main() { charalphabet; printf("Enter an alphabet"); putchar('n'); /* move to next line */ alphabet = getchar(); if (isupper(alphabet)) putchar(tolower(alphabet)); else putchar(toupper(alphabet)); }