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 Calculate BMI and BMI Category

The Body Mass Index (BMI) is defined as ratio of the weight of a person(in kilograms) to the square of the height(in meters). Write a C program that receives weight and height, calculates the BMI, and reports the BMI category as per the following table:

BMI CategoryBMI
Starvation < 15
Anorexic15.1 to 17.5
Underweight17.6 to 18.5
Ideal18.6 to 24.9
Overweight25 to 25.9
Obese30 to 30.9
Morbidly Obese>= 40

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

Formula To Calculate Body Mass Index (BMI)

bmi = weight / (height x height);

Logic To Calculate BMI and BMI Category

We ask the user to enter weight in kelogram(kg) and height in meters(m). Then using above formula we calculate BMI(Body Mass Index). Next, by referring to the chart below we display the BMI category.

BMI Category

For BMI Category Ideal:
bmi >= 18.6 && bmi <= 24.9

Expected Output for the Input

User Input:
Enter height in meter
1.4
Enter weight in kg
60

Output:
Your Body Mass Index(BMI) is 30.612246
Your BMI category is: Obese

Video Tutorial: C Program To Calculate BMI and BMI Category


[youtube https://www.youtube.com/watch?v=Lz_o-l5vNwA]

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

Source Code: C Program To Calculate BMI and BMI Category

#include < stdio.h >
int main()
{
    float height, weight, bmi;

    printf("Enter height in meter\n");
    scanf("%f", &height);

    printf("Enter weight in kg\n");
    scanf("%f", &weight);

    bmi = weight / (height * height);

    printf("Your Body Mass Index(BMI) is %f\n", bmi);

    if(bmi < 15)
    {
        printf("Your BMI category is: Starvation\n");
    }
    else if(bmi >= 15.1 && bmi <= 17.5)
    {
        printf("Your BMI category is: Anorexic\n");
    }
    else if(bmi >= 17.6 && bmi <= 18.5)
    {
        printf("Your BMI category is: Underweight\n");
    }
    else if(bmi >= 18.6 && bmi <= 24.9)
    {
        printf("Your BMI category is: Ideal\n");
    }
    else if(bmi >= 25 && bmi <= 25.9)
    {
        printf("Your BMI category is: Overweight\n");
    }
    else if(bmi >= 30 && bmi <= 30.9)
    {
        printf("Your BMI category is: Obese\n");
    }
    else if(bmi >= 40)
    {
        printf("Your BMI category is: Morbidly Obese\n");
    }
    else
    {
        printf("Wrong entry\n");
    }

    return 0;
}

Output 1:
Enter height in meter
0.8
Enter weight in kg
80
Your Body Mass Index(BMI) is 125.000000
Your BMI category is: Morbidly Obese

Output 2:
Enter height in meter
1.4
Enter weight in kg
50
Your Body Mass Index(BMI) is 25.510204
Your BMI category is: Overweight

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 Display Zodiac Sign for Given Date of Birth

Write a C program that receives month and date of birth as input and prints the corresponding Zodiac sign or Astrological sign or Sun Sign based on the following table:

Sun SignFrom – To
CapricornDecember 22 – January 19
AquariusJanuary 20 – February 17
PiscesFebruary 18 – March 19
AriesMarch 20 – April 19
TaurusApril 20 – May 20
GeminiMay 21 – June 20
CancerJune 21 – July 22
LeoJuly 23 – August 22
VirgoAugust 23 – September 22
LibraSeptember 23 – October 22
ScorpioOctober 23 – November 21
SagittariusNovember 22 – December 21

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

Logic To Display Zodiac Sign for Given Date of Birth

Example, for Sun sign Capricorn, date lies between December 22 – January 19. So using &&(AND) and ||(OR) operators:

(month == 12 && day >= 22) || (month == 1 && day <= 19)

months

Expected Output for the Input

User Input:
Enter your birth month(1-12)
4
Enter your birth day
22

Output:
Your Zodiac Sign based on your Birth date is Taurus

Video Tutorial: C Program To Display Zodiac Sign for Given Date of Birth


[youtube https://www.youtube.com/watch?v=P-FgOrNC_EA]

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

Source Code: C Program To Display Zodiac Sign for Given Date of Birth

#include < stdio.h >

int main()
{
    int m, day;

    printf("Enter your birth month(1-12)\n");
    scanf("%d", &m);

    printf("Enter your birth day\n");
    scanf("%d", &day);

    if( (m == 12 && day >= 22) || (m == 1 && day <= 19) )
    {
        printf("Your Zodiac Sign based on your Birth date is Capricorn\n");
    }
    else if( (m == 1 && day >= 20) || (m == 2 && day <= 17) )
    {
        printf("Your Zodiac Sign based on your Birth date is Aquarius\n");
    }
    else if( (m == 2 && day >= 18) || (m == 3 && day <= 19) )
    {
        printf("Your Zodiac Sign based on your Birth date is Pisces\n");
    }
    else if( (m == 3 && day >= 20) || (m == 4 && day <= 19) )
    {
        printf("Your Zodiac Sign based on your Birth date is Aries\n");
    }
    else if( (m == 4 && day >= 20) || (m == 5 && day <= 20) )
    {
        printf("Your Zodiac Sign based on your Birth date is Taurus\n");
    }
    else if( (m == 5 && day >= 21) || (m == 6 && day <= 20) )
    {
        printf("Your Zodiac Sign based on your Birth date is Gemini\n");
    }
    else if( (m == 6 && day >= 21) || (m == 7 && day <= 22) )
    {
        printf("Your Zodiac Sign based on your Birth date is Cancer\n");
    }
    else if( (m == 7 && day >= 23) || (m == 8 && day <= 22) )
    {
        printf("Your Zodiac Sign based on your Birth date is Leo\n");
    }
    else if( (m == 8 && day >= 23) || (m == 9 && day <= 22) )
    {
        printf("Your Zodiac Sign based on your Birth date is Virgo\n");
    }
    else if( (m == 9 && day >= 23) || (m == 10 && day <= 22) )
    {
        printf("Your Zodiac Sign based on your Birth date is Libra\n");
    }
    else if( (m == 10 && day >= 23) || (m == 11 && day <= 21) )
    {
        printf("Your Zodiac Sign based on your Birth date is Scorpio\n");
    }
    else if( (m == 11 && day >= 22) || (m == 12 && day <= 21) )
    {
        printf("Your Zodiac Sign based on your Birth date is Sagittarius\n");
    }
    else
    {
        printf("Invalid Birth date entered\n");
    }
    return 0;
}

Output 1:
Enter your birth month(1-12)
6
Enter your birth day
1
Your Zodiac Sign based on your Birth date is Gemini

Output 2:
Enter your birth month(1-12)
9
Enter your birth day
1
Your Zodiac Sign based on your Birth date is Virgo

zodiac sign and date of birth

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 Convert (R, G, B) To (C, M, Y, K) Color Format

In digital world colors are specified in Red-Green-Blue (RGB) format, with values of R, G, B varying on an integer scale from 0 to 255. In print publishing the colors are mentioned in Cyan-Magenta-Yellow-Black (CMYK) format, with values of C, M, Y and K varying on a real scale from 0.0 to 1.0. Write a C program that converts RGB color to CMYK color as per the following formulae:

White = Max(Red/255, Green/255, Blue/255);
Cyan = (White – Red/255) / White;
Magenta = (White – Green/255) / White;
Yellow = ( White – Blue/255) / White;
Black = 1-White

Note that if the RGB values are all 0, then the CMY values are all 0 and the K value is 1.

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

Expected Output for the Input

User Input:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
41
14
41

Output:
CMYK = (0.000000, 0.658537, 0.000000, 0.839216)

Video Tutorial: C Program To Convert (R, G, B) To (C, M, Y, K) Color Format


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

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

Source Code: C Program To Convert (R, G, B) To (C, M, Y, K) Color Format

#include < stdio.h >

int main()
{
    float red, green, blue;
    float white, cyan, magenta, yellow, black;
    float max;

    printf("Enter values for Red, Green and Blue(RGB) in Range 0 - 255\n");
    scanf("%f%f%f", &red, &green, &blue);

    if(red == 0 && green == 0 && blue == 0)
    {
        cyan   = magenta = yellow = 0;
        black  = 1;
        black  = 1;
    }
    else
    {
        red    = red   / 255;
        green  = green / 255;
        blue   = blue  / 255;

        max    = red;
        if(green > max)
        {
            max = green;
        }

        if(blue > max)
        {
            max = blue;
        }

        white   = max;

        cyan    = (white - red)  / white;
        magenta = (white - green)/ white;
        yellow  = (white - blue) / white;

        black   = 1 - white;

    }

    printf("CMYK = (%f, %f, %f, %f)\n",
            cyan, magenta, yellow, black);

    return 0;
}

Output 1:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
255
255
255
CMYK = (0.000000, 0.000000, 0.000000, 0.000000)

Output 2:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
0
0
0
CMYK = (0.000000, 0.000000, 0.000000, 1.000000)

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 Determine Leap Year or Not using Logical Operators

Any year is entered through the keyboard, write a C program to determine whether the year is a leap year or not. Use the logical operators && and ||.

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

Other Leap Year C Programs

C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator

Leap Year Logic

1. If a year is a century year(year ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year.
2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.

Century year is determined by modulo division of the entered year by 100. Example: year%100 == 0. If its century year, it must also be perfectly divisible by 400. i.e., year%400 == 0
(year%100 == 0 && year%400 == 0)

If the year is not century year, then it must be perfectly divisible by 4, for the year to be a leap year.
(year%100 != 0 && year%4 == 0)

Expected Output for the Input

User Input:
Enter a year
2024

Output:
2024 is a leap year

Video Tutorial: C Program To Determine Leap Year or Not using Logical Operators


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

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

Source Code: C Program To Determine Leap Year or Not using Logical Operators

#include < stdio.h >

int main()
{
    int year;

    printf("Enter a year\n");
    scanf("%d", &year);

    if( (year%100 == 0 && year%400 == 0) ||
        (year%100 != 0 && year%4   == 0)   )
    {
        printf("%d is a leap year\n", year);
    }
    else
    {
        printf("%d is not a leap year\n", year);
    }

    return 0;
}

Output 1:
Enter a year
2020
2020 is a leap year

Output 2:
Enter a year
2021
2021 is not a leap year

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