C language if condition with characters examples | set 1

Set 1:Q1-Q5      Set 2:Q6-Q10      Set 3:Q11-Q15

Question:1

Write a C program to take input for a character, check and print whether the character is an alphabet or not?

Sol:

#include <stdio.h>
 int main()
 {
     char ch;
     printf("Enter any character ");
     scanf("%c",&ch);
     if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
         printf("it is an alphabet");
     else
         printf("it is not an alphabet");
 return 0;
 }

Output:

Enter any character h
it is an alphabet

Enter any character d
it is an alphabet

Enter any character 5
it is not an alphabet

Question:2

Write a C program to take input for a character , check and print whether the character is an upper case alphabet or not?

Sol:

#include <stdio.h>
 int main()
 {
     char ch;
     printf("Enter any character ");
     scanf("%c",&ch);
     if(ch>='A' && ch<='Z')
         printf("it is an upper case alphabet");
     else
         printf("it is not an upper case alphabet");
 return 0;
 }

Output:

Enter any character D
it is an upper case alphabet

Enter any character e
it is not an upper case alphabet

Question:3

Write a C program to take input for a character , check and print whether the character is a lower case alphabet or not?

Sol:

#include <stdio.h>
 int main()
 {
     char ch;
     printf("Enter any character ");
     scanf("%c",&ch);
     if(ch>='a' && ch<='z')
         printf("it is a lower case alphabet");
     else
         printf("it is not a lower case alphabet");
 return 0;
 }

Output:

Enter any character d
it is a lower case alphabet

Enter any character D
it is not a lower case alphabet

Question:4

Write a C program to take input for a character , check and print whether the character is a digit or not?

Sol:

#include <stdio.h>
 int main()
 {
     char ch;
     printf("Enter any character ");
     scanf("%c",&ch);
     if(ch>='0' && ch<='9')
         printf("it is a digit");
     else
         printf("it is not a digit");
 return 0;
 }

Output:

Enter any character 5
it is a digit

Enter any character y
it is not a digit

Question:5

Write a C program to take input for a character check and print whether the character is a vowel or not?

Sol:

#include <stdio.h>

int main()
{
    char ch;
    printf("Enter any character ");
    scanf("%c",&ch);
    if(ch=='A' || ch=='a' || ch=='E' || ch=='e' || ch=='I' || ch=='i' || ch=='O' || ch=='o' || ch=='U' || ch=='u' )
        printf("it is a vowel");
    else
        printf("it is not a vowel");

    return 0;
}

Output:

Enter any character k
it is not a vowel

Enter any character e
it is a vowel

Set 1:Q1-Q5      Set 2:Q6-Q10      Set 3:Q11-Q15

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects