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

C Program To Determine Weight Class of a Boxer

In boxing the weight class of a boxer is decided as per the following table. Write a C program that receives weight as input and prints out the boxer’s weight class.

Boxer Class Weight In Pounds
Flyweight < 115
Bantamweight 115 – 121
Featherweight 122 – 153
Middleweight 154 – 189
Heavyweight >= 190

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

Expected Output for the Input

User Input:
Enter boxers weight in pounds
500

Output:
Boxer is of weight class Heavyweight

Video Tutorial: C Program To Determine Weight Class of a Boxer


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

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

Source Code: C Program To Determine Weight Class of a Boxer

#include < stdio.h >

int main()
{
    int wt;

    printf("Enter boxers weight in pounds\n");
    scanf("%d", &wt);

    if(wt < 115)
    {
        printf("Boxer is of weight class Flyweight\n");
    }
    else if(wt >= 115 && wt <= 121)
    {
        printf("Boxer is of weight class Bantamweight\n");
    }
    else if(wt >= 122 && wt <= 153)
    {
        printf("Boxer is of weight class Featherweight\n");
    }
    else if(wt >= 154 && wt <= 189)
    {
        printf("Boxer is of weight class Middleweight\n");
    }
    else if(wt >= 190)
    {
        printf("Boxer is of weight class Heavyweight\n");
    }

    return 0;
}

Output 1:
Enter boxers weight in pounds
108
Boxer is of weight class Flyweight

Output 2:
Enter boxers weight in pounds
120
Boxer is of weight class Bantamweight

Output 3:
Enter boxers weight in pounds
140
Boxer is of weight class Featherweight

Output 4:
Enter boxers weight in pounds
180
Boxer is of weight class Middleweight

Output 5:
Enter boxers weight in pounds
200
Boxer is of weight class Heavyweight

boxers weight class

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 Grade of Steel

A certain grade of steel is graded according to the following conditions:

1. Hardness must be greater than 50.
2. Carbon content must be less than 0.7
3. Tensile strength must be greater than 5600

The grades are as follows:

Grade is 10, if all three conditions are met.
Grade is 9, if conditions 1 and 2 are met.
Grade is 8, if conditions 2 and 3 are met.
Grade is 7, if conditions 1 and 3 are met.
Grade is 6, if only one condition is met.
Grade is 5, if none of the conditions are met.

Write a C program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.

Note: Tensile strength is the maximum stress that a material can withstand while being stretched or pulled before breaking.

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

Expected Output for the Input

User Input:
Enter values of hardness, tensile Strength and Carbon Content in Steel
55
5601
0.6

Output:
Steel Grade is 10

Video Tutorial: C Program To Find Grade of Steel


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

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

Source Code: C Program To Find Grade of Steel

#include<stdio.h>

int main()
{
    int hardness, ts;
    float carbon;

    printf("Enter values of hardness, tensile Strength 
            and Carbon Content in Steel\n");
    scanf("%d %d %f", &hardness, &ts, &carbon);

    if(hardness > 50 && carbon < 0.7 && ts > 5600)
    {
        printf("Steel Grade is 10\n");
    }
    else if(hardness > 50 && carbon < 0.7)
    {
        printf("Steel Grade is 9\n");
    }
    else if(carbon < 0.7 && ts > 5600)
    {
        printf("Steel Grade is 8\n");
    }
    else if(hardness > 50 && ts > 5600)
    {
        printf("Steel Grade is 7\n");
    }
    else if(hardness > 50 || carbon < 0.7 || ts > 5600)
    {
        printf("Steel Grade is 6\n");
    }
    else if(hardness < 50 && carbon > 0.7 && ts < 5600)
    {
        printf("Steel Grade is 5\n");
    }

    return 0;
}

Output 1:
Enter values of hardness, tensile Strength and Carbon Content in Steel
60
6000
0.5
Steel Grade is 10

Output 2:
Enter values of hardness, tensile Strength and Carbon Content in Steel
80
5000
0.5
Steel Grade is 9

Output 3:
Enter values of hardness, tensile Strength and Carbon Content in Steel
40
6000
0.5
Steel Grade is 8

Output 4:
Enter values of hardness, tensile Strength and Carbon Content in Steel
60
8000
0.8
Steel Grade is 7

Output 5:
Enter values of hardness, tensile Strength and Carbon Content in Steel
51
5000
0.8
Steel Grade is 6

Output 6:
Enter values of hardness, tensile Strength and Carbon Content in Steel
41
5000
0.8
Steel Grade is 5

Update To The Program

If we input:
Hardness: 50
Tensile Strength: 5600
Carbon Content: 0.7

Output will be: Steel Grade is 6.

If you check with Grade 6 condition inside if condition: ( hardness > 50 || carbon < 0.7 || ts > 5600 ) None of the condition is true. Condition for Grade 6 is: Grade is 6, if only one condition is met.

So our output is wrong. But why is it showing wrong result?

#include<stdio.h>

int main()
{
    float x = 0.7;

    printf("%d\n", sizeof(x));
    printf("%d\n", sizeof(0.7));
    printf("%d\n", sizeof(0.7f));

    return 0;
}

Output:
4
8
4

As we can clearly see 0.7 is treated as double. We can typecase it using 0.7f. So in our steel program, variable declaration(float carbon) and comparison with 0.7(which is double) is causing the bug.

With this knowledge we can rewrite our program as follows:

Source Code: C Program To Find Grade of Steel

#include<stdio.h>

int main()
{
    int hardness, ts;
    double carbon;

    printf("Enter values of hardness, tensile Strength and Carbon Content in Steel\n");
    scanf("%d%d%lf", &hardness, &ts, &carbon);

    if(hardness > 50 && carbon < 0.7 && ts > 5600)
    {
        printf("Steel Grade is 10\n");
    }
    else if(hardness > 50 && carbon < 0.7)
    {
        printf("Steel Grade is 9\n");
    }
    else if(carbon < 0.7 && ts > 5600)
    {
        printf("Steel Grade is 8\n");
    }
    else if(hardness > 50 && ts > 5600)
    {
        printf("Steel Grade is 7\n");
    }
    else if(hardness > 50 || carbon < 0.7 || ts > 5600)
    {
        printf("Steel Grade is 6\n");
    }
    else if(hardness < 50 && carbon > 0.7 && ts < 5600)
    {
        printf("Steel Grade is 5\n");
    }
    else
    {
        printf("Did not match any criteria\n");
    }

    return 0;
}

Output 1:
Enter values of hardness, tensile Strength and Carbon Content in Steel
50
5600
0.7
Did not match any criteria

Output 2:
Enter values of hardness, tensile Strength and Carbon Content in Steel
41
5000
0.8
Steel Grade is 5

In above program we’re declaring carbon as double and using %lf format specifier to take value of carbon as input from the user.

Typecasing 0.7 to floating point value

#include<stdio.h>

int main()
{
    int hardness, ts;
    float carbon;

    printf("Enter values of hardness, tensile Strength and Carbon Content in Steel\n");
    scanf("%d%d%f", &hardness, &ts, &carbon);

    if(hardness > 50 && carbon < 0.7f && ts > 5600)
    {
        printf("Steel Grade is 10\n");
    }
    else if(hardness > 50 && carbon < 0.7f)
    {
        printf("Steel Grade is 9\n");
    }
    else if(carbon < 0.7f && ts > 5600)
    {
        printf("Steel Grade is 8\n");
    }
    else if(hardness > 50 && ts > 5600)
    {
        printf("Steel Grade is 7\n");
    }
    else if(hardness > 50 || carbon < 0.7f || ts > 5600)
    {
        printf("Steel Grade is 6\n");
    }
    else if(hardness < 50 && carbon > 0.7f && ts < 5600)
    {
        printf("Steel Grade is 5\n");
    }
    else
    {
        printf("Did not match any criteria\n");
    }

    return 0;
}

Output 1:
Enter values of hardness, tensile Strength and Carbon Content in Steel
50
5600
0.7
Did not match any criteria

Output 2:
Enter values of hardness, tensile Strength and Carbon Content in Steel
41
5000
0.8
Steel Grade is 5

Here we are typecasing 0.7 into floating point value by using 0.7f in else if conditions, which matches with the data type of variable carbon.

Related Read:
Comparing Floating Point Variable With a Value In C Programming

Video Tutorial: Comparing Floating Point Variable With a Value In C Programming


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

YouTube Link: https://www.youtube.com/watch?v=DXh6TsxLTME [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