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 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

Logical Operators In C

Logical Operators in C programming language return true(non-zero number) or false(0) value. Logical AND(&&) and logical OR(||) works on 2 operands. But logical NOT(!) works on single operand.

Related Read:
Relational Operators In C

Logical Operators

&& – Logical AND Operator.
|| – Logical OR Operator.
! – Logical NOT Operator.

&& – Logical AND Operator.

 
#include < stdio.h >

int main()
{
    int a;

    a = ( 1 && 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >

int main()
{
    int a;

    a = ( 0 && 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

For logical AND(&&) both operands or expressions must yield to true. If any one condition is false(0), then it’ll return false(0).

|| – Logical OR Operator.

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 1 || 1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 1 || 0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

 
#include < stdio.h >
int main()
{
    int a;

    a = ( 0 || 0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

Logical OR(||) returns true(any non-zero number) if either one condition/operand is true. It returns false(0) only when both the conditions / operands are false(0).

! – Logical NOT Operator.

 
#include < stdio.h >
int main()
{
    int a;

    a = ( !1 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

 
#include < stdio.h >
int main()
{
    int a;

    a = ( !0 );

    printf("value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

Logical NOT(!) returns true if the condition is false. It returns false if the condition is true. It just negates the Boolean value given to it.

Logical Operators In C


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

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


Logical Operators combined with Relational Operators

&& – Logical AND Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) && (b > 50) );

    printf("Value of a is %d\n", a);

    return 0;
}

Output:
value of a is 1

a is true because both b is equal to 100 is true and b is greater than 50 is true.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) && (b > 150) );

    printf("Value of a is %d\n", a);

    return 0;
}

Output:
value of a is 0

a is false(0) because b is equal to 100 is true but b is greater than 150 is false.

|| – Logical OR Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( (b == 0) || (b > 50) );

    printf("%d\n", a);

    return 0;
}

Output:
value of a is 1

Value of a is true, because b is equal to true. In logical OR(||) if one condition is true, then it returns true. It returns false(0) only when both the conditions / operands are false(0).

! – Logical NOT Operator.

 
#include < stdio.h >

int main()
{
    int a, b = 100;

    a = ( !(b == 0) );

    printf("%d\n", a);

    return 0;
}

Output:
value of a is 0

Value of a is false(0). Because b is equal to 100 is true. When true value is given to NOT(!) it’ll return false(0). When false value is supplied to NOT it’ll return true.

Note: = is assignment operator. == is equality operator.

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

$or (Union) Operator: MongoDB

Today lets learn about $or operator.

In set theory, the union (denoted by ∪) of a collection of sets is the set of all distinct elements in the collection. It is one of the fundamental operations through which sets can be combined and related to each other.

Union of two sets
The union of two sets A and B is the collection of points which are in A or in B or in both A and B. In symbols,

union-of-two-sets
For example, if A = {1, 3, 5, 7} and B = {1, 2, 4, 6} then A ∪ B = {1, 2, 3, 4, 5, 6, 7}.

$or (Union) Operator: MongoDB


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

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



Documents in our collection
test database, names collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53be5d8604cc1cb0a7bfc3c5"), "name" : "Farhan" }
{ "_id" : ObjectId("53be5d9204cc1cb0a7bfc3c6"), "name" : "Gangs" }
{ "_id" : ObjectId("53be5d9904cc1cb0a7bfc3c7"), "name" : "Hum" }
{ "_id" : ObjectId("53be5e3704cc1cb0a7bfc3c8"), "name" : 25 }
{
        "_id" : ObjectId("53beaa0f6a8a31dc255d4589"),
        "name" : "Satish",
        "age" : 27
}

Related Read: $exists, $type, $regex operators: MongoDB

1
2
3
> db.names.find({$or: [{"name": {$regex: "^E"}}, {"age": {$exists: true}}]});
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53beaa0f6a8a31dc255d4589"), "name" : "Satish", "age" : 27 }

$or is a prefix operator. It takes array as it’s value. The array can contain any number of objects for the union. Mongo Shell fetches all the documents which matches any of the documents which the individual objects inside the array points to.

1
2
3
> db.names.find({$or: [{"name": {$regex: "^E"}}, {"name": {$type: 1}}]});
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53be5e3704cc1cb0a7bfc3c8"), "name" : 25 }

$type: 1, points to Boolean value.

Note: Next we’ll learn how to make use of $and operator.