C language if condition with characters examples | set 3

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

Question: 11

Write a C program to take input for a character check and print whether it is an alphabet, digit or a special character. If it is an alphabet also print whether it is an upper case alphabet or lower case alphabet?

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\n");
        if(ch>='A' && ch<='Z')
            printf("it is an upper case alphabet");
        else
            printf("It is a lower case alphabet");
    }
    else
        if(ch>='0' && ch<='9')
        printf("it is a digit");
    else
        printf("it is a special character");


    return 0;
}

Output:

case 1:

Enter any character A
It is an alphabet
it is an upper case alphabet

case 2:

Enter any character d
It is an alphabet
It is a lower case alphabet

case 3:

Enter any character 5
it is a digit

case 4:
Enter any character #
it is a special character

Question: 12

Write a C program to take input for a character check and print whether it is an alphabet or not, if it is alphabet check and print whether it is an upper case or lower case alphabet. If it is an upper case alphabet print its lower form and vice versa?

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\n");
        if(ch>='A' && ch<='Z')
        {
            printf("it is an upper case alphabet\n");
            printf("its lower form is %c\n",ch+32);
        }
        else
        {
            printf("It is a lower case alphabet\n");
            printf("its upper form is %c\n",ch-32);
        }
    }
    else
        printf("it is a not an alphabet");


    return 0;
}

Output:

case 1:

Enter any character A
It is an alphabet
it is an upper case alphabet
its lower form is a

case 2:

Enter any character d
It is an alphabet
It is a lower case alphabet
its upper form is D

case 3:

Enter any character 5
it is a not an alphabet

Question: 13

Write a C program to take input for a character check and print whether it is an alphabet or not, if it is alphabet print its position in alphabetical order?

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\n");
        if(ch>='A' && ch<='Z')
        {
            printf("it is an upper case alphabet\n");
            printf("its position is  %d\n",(char)(ch-64));
        }
        else
        {
            printf("It is a lower case alphabet\n");
            printf("its position is %d\n",(char)(ch-96));
        }
    }
    else
        printf("it is a not an alphabet");


    return 0;
}

Output:

case 1:

Enter any character d
It is an alphabet
It is a lower case alphabet
its position is 4

case 2:

Enter any character E
It is an alphabet
it is an upper case alphabet
its position is 5

case 3:

Enter any character 3
it is a not an alphabet

Question: 14

WAP to take input for 2 numbers and an operator (+,-,*,/). Based on the entered operator calculate and display the result.

Sol:

#include <stdio.h>
int main()
{
    float a,b,c;
    char op;
    printf("Enter 2 nos ");
    scanf("%f %f",&a,&b);
    printf("Enter the operator (+, -, *, /)");
    fflush(stdin);
    scanf("%c",&op);
    if(op=='+')
    {
        c=a+b;
        printf("Sum = %f",c);
    }
    else
        if(op=='-')
    {
        if(a>b)
            c=a-b;
        else
            c=b-a;
        printf("Diff = %f",c);
    }
    else
        if(op=='*')
    {
        c=a*b;
        printf("Prod = %f",c);
    }
    else
        if(op=='/')
    {
        c=a/b;
        printf("div = %f",c);
    }
    else
        printf("Invalid operator");

    return 0;
}

Output:

case :1

Enter 2 nos 10
20
Enter the operator (+, -, *, /)+
Sum = 30.000000

case 2:

Enter 2 nos 25
6
Enter the operator (+, -, *, /)*
Prod = 150.000000

case 3:

Enter 2 nos 10
20
Enter the operator (+, -, *, /))
Invalid operator

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