Using Macros Find Arithmetic Mean, Absolute Value, Biggest of 3 number and convert upper case alphabet to lower case: C Program

Problem Statement: Write down macro definitions for the following:

1. To find arithmetic mean of two numbers.
2. To find absolute value of a number.
3. To convert an upper case alphabet to lower case.
4. To obtain the biggest of three numbers.

Related Read:
Switch Case Default In C Programming Language
Macros With Arguments: C Program
do-while Loop In C Programming Language

Video Tutorial: Using Macros Find Arithmetic Mean, Absolute Value, Biggest of 3 number and convert upper case alphabet to lower case: C Program


[youtube https://www.youtube.com/watch?v=JvLQTBD7S3Q]

YouTube Link: https://www.youtube.com/watch?v=JvLQTBD7S3Q [Watch the Video In Full Screen.]

Source Code: Using Macros Find Arithmetic Mean, Absolute Value, Biggest of 3 number and convert upper case alphabet to lower case: C Program

#include<stdio.h>

#define MEAN(a, b)  ( (a + b) / 2.0 )
#define ABS(num)    ( (num > 0) ? num : (num * -1) )
#define LOWER(ch)   ( ch + 32 )
#define BIGGEST(a, b, c) ( (a > b && a > c) ? a : ( (b > c) ? b : c ) )

int main()
{
    int choice, num, repeat;
    float a, b, c;
    char ch;

    do
    {
        printf("1. Find Arithmetic Mean of 2 numbers\n");
        printf("2. Find Absolute Value of a number\n");
        printf("3. Convert a Uppercase letter to lowercase\n");
        printf("4. Find Biggest of 3 numbers\n");

        printf("\nEnter your choice\n");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1: printf("Enter 2 numbers\n");
                    scanf("%f%f", &a, &b);
                    printf("Arithmetic Mean: %0.2f\n", MEAN(a, b));
                    break;
            case 2: printf("Enter a integer number\n");
                    scanf("%d", &num);
                    printf("Absolute value of |%d| is %d\n", num, ABS(num));
                    break;
            case 3: printf("Enter a uppercase alphabet\n");
                    fflush(stdin);
                    scanf("%c", &ch);

                    if( ch >= 65 && ch <= 90)
                        printf("To Lowercase: %c\n", LOWER(ch));
                    else
                        printf("Enter a valid uppercase alphabet\n");

                    break;
            case 4: printf("Enter 3 numbers\n");
                    scanf("%f%f%f", &a, &b,&c);
                    printf("Biggest no is %0.2f\n", BIGGEST(a, b, c));
                    break;
            default: printf("Please enter valid choice\n");
        }

        printf("\nDo you want to continue? Ans: 0 or 1\n");
        scanf("%d", &repeat);

        printf("\n");

    }while(repeat);

    return 0;
}

Output:
1. Find Arithmetic Mean of 2 numbers
2. Find Absolute Value of a number
3. Convert a Uppercase letter to lowercase
4. Find Biggest of 3 numbers

Enter your choice
1
Enter 2 numbers
41
14
Arithmetic Mean: 27.50

Do you want to continue? Ans: 0 or 1
1

1. Find Arithmetic Mean of 2 numbers
2. Find Absolute Value of a number
3. Convert a Uppercase letter to lowercase
4. Find Biggest of 3 numbers

Enter your choice
2
Enter a integer number
5
Absolute value of |5| is 5

Do you want to continue? Ans: 0 or 1
1

1. Find Arithmetic Mean of 2 numbers
2. Find Absolute Value of a number
3. Convert a Uppercase letter to lowercase
4. Find Biggest of 3 numbers

Enter your choice
2
Enter a integer number
-5
Absolute value of |-5| is 5

Do you want to continue? Ans: 0 or 1
1

1. Find Arithmetic Mean of 2 numbers
2. Find Absolute Value of a number
3. Convert a Uppercase letter to lowercase
4. Find Biggest of 3 numbers

Enter your choice
3
Enter a uppercase alphabet
S
To Lowercase: s

Do you want to continue? Ans: 0 or 1
1

1. Find Arithmetic Mean of 2 numbers
2. Find Absolute Value of a number
3. Convert a Uppercase letter to lowercase
4. Find Biggest of 3 numbers

Enter your choice
3
Enter a uppercase alphabet
$
Enter a valid uppercase alphabet

Do you want to continue? Ans: 0 or 1
1

1. Find Arithmetic Mean of 2 numbers
2. Find Absolute Value of a number
3. Convert a Uppercase letter to lowercase
4. Find Biggest of 3 numbers

Enter your choice
4
Enter 3 numbers
20
40
50
Biggest no is 50.00

Do you want to continue? Ans: 0 or 1
0

Formula and Logic

1. Arithmetic mean: If user enters two numbers, then we add those 2 numbers and divide it by 2 to get the result. If user inputs 3 numbers, we first add all these 3 numbers and divide it by 3 to get mean.

In this program, according to problem statement, we need to add 2 numbers and divide it by 2 to get the mean of those 2 user input numbers.

Since we’re taking 2 floating point numbers, we’re dividing the sum of two numbers by 2.0 and not by integer 2. If we divide float or double number with integer number 2, then there is possibility of getting wrong result.

Calculate Sum and Average of N Numbers without using Arrays: C Program

Average and Mean are same in mathematics.

2. Absolute Value: Absolute value is like distance. In whichever direction you move there can only be positive distance. You can’t walk negative 5 kilometre!

So for any integer input by the user, we return it’s positive value by multiplying it by -1, in case user input number is negative.

C Program To Find Absolute Value of a Number

Note: We could have used built-in method abs() from the library file stdlib.h to get absolute value of user input number. But to use a single built-in method abs() we must include all the things present in stdlib.h file, so we better write definition to calculate absolute value ourselves.

3. Convert a Uppercase alphabet to lowercase: We should know the ASCII value of A and Z, as well as ASCII value of a and z to get the result.

C Program To Print All ASCII Characters and Code

ASCII value range of upper case alphabets:
ASCII value of A is 65.
ASCII value of B is 66.
ASCII value of C is 67.

and so on till Z ..

ASCII value of Z is 90.

ASCII value range of lower case alphabets:
ASCII value of a is 97.
ASCII value of b is 98.
ASCII value of c is 99.

and so on till z ..

ASCII value of z is 122.

If you observe the ASCII values properly, you’ll know that there is a difference of 32 between a and A in it’s ASCII value. So, if user inputs a capital letter, then we simply add 32 to it and display the character – which will be its corresponding lowercase alphabet.

Note: Since we might start to input information from the keyboard repeatedly inside do-while block, scanf() method keeps checking the input buffer. And often times it gets confused with the input buffer and thinks that the user has pressed the enter key. To avoid that we flush out the previous buffer present in input device(ex: keyboard) using function fflush(). fflush takes stdin as argument, so that it can clear the buffer of standard input device. fflush(stdin);

4. Biggest of 3 Numbers: Here we make use of nested ternary or conditional operator. If a is greater than b and c, then we return value of a. ORELSE if b is greater than c, then we return the value of b, else we return the value of c.

Biggest of 3 Numbers Using Ternary Operator: C

Note: We can continue writing macro expansion in next line by making use of macro continuation operator(\). You can see that we’ve broken the line and written the code in next line inside macro expansion of BIGGEST(a, b, c).

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Using Macros Check For Uppercase / Lowercase and Alphabet or Not and Biggest of 2 Numbers: C Program

Problem State: Write down macro definitions for the following:
1. To test whether a character is a small case letter or not.
2. To test whether a character is an upper case letter or not.
3. To test whether a character is an alphabet or not. Make use of the macros you defined in 1 and 2 above.
4. To obtain the bigger of two numbers.

Related Read:
Switch Case Default In C Programming Language
Macros With Arguments: C Program
Biggest of Two Numbers Using Ternary Operator: C
C Program To Print Uppercase Alphabet(A-Z) using While loop
C Program To Print Lowercase Alphabet(a-z) using While loop
C Program To Check Whether a Character is an Alphabet or Not

Problem Statement Analysis

1. We need to write 4 Macro definitions.
2. We must write macros to find upper and lower case, and then make use of these two macros to find if user entered character is alphabet or not.
3. We can find biggest of 2 numbers by using Ternary / conditional operator in macro expansion.

Video Tutorial: Using Macros Check For Uppercase / Lowercase and Alphabet or Not and Biggest of 2 Numbers: C Program


[youtube https://www.youtube.com/watch?v=EEGLYe2buxk]

YouTube Link: https://www.youtube.com/watch?v=EEGLYe2buxk [Watch the Video In Full Screen.]

Source Code: Using Macros Check For Uppercase / Lowercase and Alphabet or Not and Biggest of 2 Numbers: C Program

#include<stdio.h>

#define isUPPER(ch) ( ch >= 'A' && ch <= 'Z' )
#define isLOWER(ch) ( ch >= 'a' && ch <= 'z' )
#define isALPHABET(ch) ( isUPPER(ch) || isLOWER(ch) )
#define BIGGEST(a, b) ( ( a > b ) ? \
                       printf("%d is the biggest\n", a) : \
                       printf("%d is the biggest\n", b) )

int main()
{
    int a, b, repeat;
    char ch, choice;

    do
    {
        printf("1. Check if entered character is upper or lower case\n");
        printf("2. Check if entered character is alphabet or not\n");
        printf("3. Find biggest of 2 numbers\n");

        printf("\nEnter your choice\n");
        scanf(" %c", &choice);

        switch(choice)
        {
            case '1': printf("\nEnter a character\n");
                      scanf(" %c", &ch);

                      if( isUPPER(ch) )
                      {
                          printf("Entered character is upper case letter\n");
                      }
                      else if( isLOWER(ch) )
                      {
                          printf("Entered character is lower case letter\n");
                      }
                      else
                      {
                          printf("Please enter a valid alphabet\n");
                      }

                      break;
            case '2': printf("\nEnter a character\n");
                      scanf(" %c", &ch);

                      if( isALPHABET(ch) )
                      {
                          printf("Entered character is an alphabet\n");
                      }
                      else
                      {
                          printf("Entered character is not an alphabet\n");
                      }
                      break;
            case '3': printf("\nEnter 2 numbers\n");
                      scanf("%d%d", &a, &b);

                      BIGGEST(a, b);

                      break;
            default:  printf("\nPlease enter valid choice\n");
        }

        printf("\n\nDo you want to continue? Ans: 0 or 1\n");
        scanf("%d", &repeat);

        printf("\n");
        fflush(stdin);

    }while(repeat);

    return 0;
}

Output:
1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
1

Enter a character
S
Entered character is upper case letter

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
1

Enter a character
s
Entered character is lower case letter

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
1

Enter a character
$
Please enter a valid alphabet

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
2

Enter a character
A
Entered character is an alphabet

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
2

Enter a character
&
Entered character is not an alphabet

Do you want to continue? Ans: 0 or 1
1

1. Check if entered character is upper or lower case
2. Check if entered character is alphabet or not
3. Find biggest of 2 numbers

Enter your choice
3

Enter 2 numbers
14
50
50 is the biggest

Do you want to continue? Ans: 0 or 1
0

Here we are using do-while loop to repeatedly show the user choices: If user enters 1, the choices are shown once again. If the user enters 0, then control exits the do-while loop.

Choice 1: Upper or Lower Case Alphabet

All the characters have ASCII value associated with it in C programming. So internally it checks the ASCII value of user entered character against the ASCII values of “A” to “Z”.

ASCII value range of upper case alphabets:
ASCII value of A is 65.
ASCII value of B is 66.
ASCII value of C is 67.

and so on till Z ..

ASCII value of Z is 90.

So all the ASCII values between 65 and 90 (including 65 and 90) are Capital letter alphabets.

Similarly, below we’ve listed the ASCII values of lower case alphabets.

ASCII value range of lower case alphabets:
ASCII value of a is 97.
ASCII value of b is 98.
ASCII value of c is 99.

and so on till z ..

ASCII value of z is 122.

So all the ASCII values between 97 and 122 (including 97 and 122) are Lower case letter alphabets.

Related Read:
C Program To Print Uppercase Alphabet(A-Z) using While loop
C Program To Print Lowercase Alphabet(a-z) using While loop

Choice 2: Alphabet or Not

According to our problem statement we need to use the macros we defined for “Choice 1” to evaluate if the user entered character is alphabet or not. So if the user entered character is upper or lower case latter than its alphabet or else its not an alphabet.

Related Read:
C Program To Check Whether a Character is an Alphabet or Not

Choice 3: Biggest of Two Numbers

Inside macro expansion we make use of ternary / conditional operator to find biggest of 2 numbers.

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

Bug in accepting character as input

When you input some data via console window and hit enter key, the enter key or the new line character gets stored inside input buffer. If you’re accepting a single character from keyboard via scanf() function, often times it gets confused with the input buffer and thinks that the user has pressed the enter key as the input character. We can avoid it in 3 ways:

1. Use double scanf() function, as illustrated in above video tutorial.
2. Use a space before %c inside scanf() method.
3. Use fflush(stdin) before every scanf() method which accepts a single character value.

Note: Since we might start to input information from the keyboard repeatedly inside do-while block, scanf() method keeps checking the input buffer. And often times it gets confused with the input buffer and thinks that the user has pressed the enter key. To avoid that we flush out the previous buffer present in input device(ex: keyboard) using function fflush(). fflush takes stdin as argument, so that it can clear the buffer of standard input device. fflush(stdin);

#include<stdio.h>

#define isUPPER(ch) ( ch >= 'A' && ch <= 'Z' )
#define isLOWER(ch) ( ch >= 'a' && ch <= 'z' )
#define isALPHABET(ch) ( isUPPER(ch) || isLOWER(ch) )
#define BIGGEST(a, b) ( ( a > b ) ? \
                       printf("%d is the biggest\n", a) : \
                       printf("%d is the biggest\n", b) )

int main()
{
    int a, b, repeat;
    char ch, choice;

    do
    {
        printf("1. Check if entered character is upper or lower case\n");
        printf("2. Check if entered character is alphabet or not\n");
        printf("3. Find biggest of 2 numbers\n");

        printf("\nEnter your choice\n");
        scanf("%c", &choice);

        switch(choice)
        {
            case '1': printf("\nEnter a character\n");
                      fflush(stdin);
                      scanf("%c", &ch);

                      if( isUPPER(ch) )
                      {
                          printf("Entered character is upper case letter\n");
                      }
                      else if( isLOWER(ch) )
                      {
                          printf("Entered character is lower case letter\n");
                      }
                      else
                      {
                          printf("Please enter a valid alphabet\n");
                      }

                      break;
            case '2': printf("\nEnter a character\n");
                      fflush(stdin);
                      scanf("%c", &ch);

                      if( isALPHABET(ch) )
                      {
                          printf("Entered character is an alphabet\n");
                      }
                      else
                      {
                          printf("Entered character is not an alphabet\n");
                      }
                      break;
            case '3': printf("\nEnter 2 numbers\n");
                      scanf("%d%d", &a, &b);

                      BIGGEST(a, b);

                      break;
            default:  printf("\nPlease enter valid choice\n");
        }

        printf("\n\nDo you want to continue? Ans: 0 or 1\n");
        scanf("%d", &repeat);

        printf("\n");
        fflush(stdin);

    }while(repeat);

    return 0;
}

In above source code we’re making use of fflush(stdin) before every scanf() method which accepts single character. fflush(stdin) flushes out the previous buffer present in input device(ex: Keyboard).

Note: We can continue writing macro expansion in next line by making use of macro continuation operator(\). You can see that we’ve broken the line and written the code in next line inside macro expansion to find biggest of 2 numbers.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print All ASCII Characters and Value using For Loop

Lets write a C program to print/display all ASCII characters and its corresponding value using For loop.

Note: In C programming language, every alphabet, number and symbol has corresponding ASCII value(a integer number representing the character).

In C programming language, there are 256 ASCII Characters and ASCII Values. i.e., from 0 to 255.

Related Read:
For Loop In C Programming Language
C Program To Print All ASCII Characters and Value

Video Tutorial: C Program To Print All ASCII Characters and Value using For Loop


[youtube https://www.youtube.com/watch?v=xTRbser_9us]

YouTube Link: https://www.youtube.com/watch?v=xTRbser_9us [Watch the Video In Full Screen.]

Source Code: C Program To Print All ASCII Characters and Value using For Loop

#include<stdio.h>

int main()
{
    int count;

    for(count = 0; count <= 255; count++)
    {
        printf("ASCII value of %c is %d\n", count, count);
    }

    return 0;
}

Output
ASCII value of is 0

ASCII value of is 1

ASCII value of is 2

ASCII value of is 3

ASCII value of is 4

ASCII value of is 5

ASCII value of is 6

ASCII value of is 7

ASCII value of is 8

ASCII value of is 9

ASCII value of
is 10

ASCII value of is 11

ASCII value of is 12

is 13

ASCII value of is 14

ASCII value of is 15

ASCII value of is 16

ASCII value of is 17

ASCII value of is 18

ASCII value of is 19

ASCII value of is 20

ASCII value of § is 21

ASCII value of is 22

ASCII value of is 23

ASCII value of is 24

ASCII value of is 25

ASCII value of is 26

ASCII value of is 27

ASCII value of is 28

ASCII value of is 29

ASCII value of is 30

ASCII value of is 31

ASCII value of is 32

ASCII value of ! is 33

ASCII value of is 34

ASCII value of # is 35

ASCII value of $ is 36

ASCII value of % is 37

ASCII value of & is 38

ASCII value of is 39

ASCII value of ( is 40

ASCII value of ) is 41

ASCII value of * is 42

ASCII value of + is 43

ASCII value of , is 44

ASCII value of is 45

ASCII value of . is 46

ASCII value of / is 47

ASCII value of 0 is 48

ASCII value of 1 is 49

ASCII value of 2 is 50

ASCII value of 3 is 51

ASCII value of 4 is 52

ASCII value of 5 is 53

ASCII value of 6 is 54

ASCII value of 7 is 55

ASCII value of 8 is 56

ASCII value of 9 is 57

ASCII value of : is 58

ASCII value of ; is 59

ASCII value of < is 60

ASCII value of = is 61

ASCII value of > is 62

ASCII value of ? is 63

ASCII value of @ is 64

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

ASCII value of [ is 91

ASCII value of \ is 92

ASCII value of ] is 93

ASCII value of ^ is 94

ASCII value of _ is 95

ASCII value of ` is 96

ASCII value of a is 97

ASCII value of b is 98

ASCII value of c is 99

ASCII value of d is 100

ASCII value of e is 101

ASCII value of f is 102

ASCII value of g is 103

ASCII value of h is 104

ASCII value of i is 105

ASCII value of j is 106

ASCII value of k is 107

ASCII value of l is 108

ASCII value of m is 109

ASCII value of n is 110

ASCII value of o is 111

ASCII value of p is 112

ASCII value of q is 113

ASCII value of r is 114

ASCII value of s is 115

ASCII value of t is 116

ASCII value of u is 117

ASCII value of v is 118

ASCII value of w is 119

ASCII value of x is 120

ASCII value of y is 121

ASCII value of z is 122

ASCII value of { is 123

ASCII value of | is 124

ASCII value of } is 125

ASCII value of ~ is 126

ASCII value of  is 127

ASCII value of Ç is 128

ASCII value of ü is 129

ASCII value of é is 130

ASCII value of â is 131

ASCII value of ä is 132

ASCII value of à is 133

ASCII value of å is 134

ASCII value of ç is 135

ASCII value of ê is 136

ASCII value of ë is 137

ASCII value of è is 138

ASCII value of ï is 139

ASCII value of î is 140

ASCII value of ì is 141

ASCII value of Ä is 142

ASCII value of Å is 143

ASCII value of É is 144

ASCII value of æ is 145

ASCII value of Æ is 146

ASCII value of ô is 147

ASCII value of ö is 148

ASCII value of ò is 149

ASCII value of û is 150

ASCII value of ù is 151

ASCII value of ÿ is 152

ASCII value of Ö is 153

ASCII value of Ü is 154

ASCII value of ¢ is 155

ASCII value of £ is 156

ASCII value of ¥ is 157

ASCII value of is 158

ASCII value of ƒ is 159

ASCII value of á is 160

ASCII value of í is 161

ASCII value of ó is 162

ASCII value of ú is 163

ASCII value of ñ is 164

ASCII value of Ñ is 165

ASCII value of ª is 166

ASCII value of º is 167

ASCII value of ¿ is 168

ASCII value of is 169

ASCII value of ¬ is 170

ASCII value of ½ is 171

ASCII value of ¼ is 172

ASCII value of ¡ is 173

ASCII value of « is 174

ASCII value of » is 175

ASCII value of is 176

ASCII value of is 177

ASCII value of is 178

ASCII value of is 179

ASCII value of is 180

ASCII value of is 181

ASCII value of is 182

ASCII value of is 183

ASCII value of is 184

ASCII value of is 185

ASCII value of is 186

ASCII value of is 187

ASCII value of is 188

ASCII value of is 189

ASCII value of is 190

ASCII value of is 191

ASCII value of is 192

ASCII value of is 193

ASCII value of is 194

ASCII value of is 195

ASCII value of is 196

ASCII value of is 197

ASCII value of is 198

ASCII value of is 199

ASCII value of is 200

ASCII value of is 201

ASCII value of is 202

ASCII value of is 203

ASCII value of is 204

ASCII value of is 205

ASCII value of is 206

ASCII value of is 207

ASCII value of is 208

ASCII value of is 209

ASCII value of is 210

ASCII value of is 211

ASCII value of is 212

ASCII value of is 213

ASCII value of is 214

ASCII value of is 215

ASCII value of is 216

ASCII value of is 217

ASCII value of is 218

ASCII value of is 219

ASCII value of is 220

ASCII value of is 221

ASCII value of is 222

ASCII value of is 223

ASCII value of α is 224

ASCII value of ß is 225

ASCII value of Γ is 226

ASCII value of π is 227

ASCII value of Σ is 228

ASCII value of σ is 229

ASCII value of µ is 230

ASCII value of τ is 231

ASCII value of Φ is 232

ASCII value of Θ is 233

ASCII value of Ω is 234

ASCII value of δ is 235

ASCII value of is 236

ASCII value of φ is 237

ASCII value of ε is 238

ASCII value of is 239

ASCII value of is 240

ASCII value of ± is 241

ASCII value of is 242

ASCII value of is 243

ASCII value of is 244

ASCII value of is 245

ASCII value of ÷ is 246

ASCII value of is 247

ASCII value of ° is 248

ASCII value of is 249

ASCII value of · is 250

ASCII value of is 251

ASCII value of is 252

ASCII value of ² is 253

ASCII value of is 254

ASCII value of   is 255

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Lowercase Alphabet or Not using Conditional Operator

Using Conditional Operator / Ternary Operator determine, whether the character entered through the keyboard is a lower case English alphabet or not.

Also Check:
C Program To Find Special Symbol or Not using Conditional Operator

Related Read:
Relational Operators In C
Logical Operators In C
Ternary Operator / Conditional Operator In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a character
a

Output:
Character entered is a lowercase English alphabet

Logic To Find Lowercase Alphabet or Not using Conditional Operator

Using Conditional Operator we write the condition, if user entered character is greater than or equal to ASCII Value 97(which corresponds to lowercase character a) and less than or equal to ASCII Value 122(which corresponds to lowercase character z).

If the condition is true, then user entered character is lower case English alphabet, if not, then its not a lowercase English alphabet.

Video Tutorial: C Program To Find Lowercase Alphabet or Not using Conditional Operator


[youtube https://www.youtube.com/watch?v=YM50IowuYhk]

YouTube Link: https://www.youtube.com/watch?v=YM50IowuYhk [Watch the Video In Full Screen.]

Source Code: C Program To Find Lowercase Alphabet or Not using Conditional Operator

#include < stdio.h >

int main()
{
    char ch;

    printf("Enter a character\n");
    scanf("%c", &ch);

    (ch >= 97 && ch <= 122) ?
    printf("Character entered is a lowercase English alphabet\n") :
    printf("Character entered is not a lowercase English alphabet\n");

    return 0;
}

Output 1:
Enter a character
$
Character entered is not a lowercase English alphabet

Output 2:
Enter a character
A
Character entered is not a lowercase English alphabet

Output 3:
Enter a character
5
Character entered is not a lowercase English alphabet

Output 4:
Enter a character
a
Character entered is a lowercase English alphabet

Output 5:
Enter a character
z
Character entered is a lowercase English alphabet

ASCII Values of Lowercase English Alphabets

ASCII value of a is 97

ASCII value of b is 98

ASCII value of c is 99

ASCII value of d is 100

ASCII value of e is 101

ASCII value of f is 102

ASCII value of g is 103

ASCII value of h is 104

ASCII value of i is 105

ASCII value of j is 106

ASCII value of k is 107

ASCII value of l is 108

ASCII value of m is 109

ASCII value of n is 110

ASCII value of o is 111

ASCII value of p is 112

ASCII value of q is 113

ASCII value of r is 114

ASCII value of s is 115

ASCII value of t is 116

ASCII value of u is 117

ASCII value of v is 118

ASCII value of w is 119

ASCII value of x is 120

ASCII value of y is 121

ASCII value of z is 122

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Check Whether a Character is Vowel or Consonant

Lets write a C program to check whether user entered character is a vowel or a consonant.

Related Read:
if else statement in C
Logical Operators In C

Note: Lowercase English alphabets a, e, i, o, u and uppercase English alphabets A, E, I, O, U are called Vowels. All other alphabets are called Consonants.

We assume that the user enters only alphabets as input for this program.

Source Code: C Program To Check Whether a Character is Vowel or Consonant

#include < stdio.h >

int main()
{
    char ch;

    printf("Enter a character\n");
    scanf("%c", &ch);

    if( ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
    {
        printf("%c is vowel\n", ch);
    }
    else
    {
        printf("%c is consonant\n", ch);
    }

    return 0;
}

Output 1:
Enter a character
A
A is vowel

Output 2:
Enter a character
B
B is consonant

Output 3:
Enter a character
e
e is vowel

Output 4:
Enter a character
f
f is consonant

Logic To Check Whether a Character is Vowel or Consonant

We ask the user to enter an alphabet and store it inside character variable ch. Using if else construct we check if the user entered alphabet is vowel or consonant. Inside if condition, we check if alphabet present in variable ch is equal to a or e or i or o or u or A or E or I or O or U. If any of these conditions are true, then it’ll return true and block inside if executes orelse else block gets executed.

In the background, c program checks the ASCII value present in variable ch with the ASCII values of a, e, i, o, u, A, E, I, O, U.

Video Tutorial: C Program To Check Whether a Character is Vowel or Consonant


[youtube https://www.youtube.com/watch?v=lJSmUKw3cMM]

YouTube Link: https://www.youtube.com/watch?v=lJSmUKw3cMM [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert