C Program To Check Leap Year Using Ternary Operator

C Program to Find if a given Year is a Leap Year or Not, using Ternary Operator.

Related Read:
Ternary Operator / Conditional Operator In C
C Program To Check Leap Year

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

We are illustrating the use of multiple nested ternary operator in this program. We have a nested ternary operator in place of expression2 as well as expression3.

Leap Year Logic

Leap Year
1. If a year is a century year(years 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.

Not Leap Year
1. If the year entered is a century year(perfectly divisible by 100), but not perfectly divisible by 400. Then it’s not a leap year.
2. A year which is not a century year and is not perfectly divisible by 4 is not a leap year.

C Program To Check Leap Year Using Nested Ternary Operator


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

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


In this video tutorial we illustrate the use of multiple nested ternary operator / conditional operator inside a ternary operator.

First we check if given year is perfectly divisible by 100. If its true, then the entered year is a century year(year ending with 00). If the year is also perfectly divisible by 400, then its leap year, if not, its not a leap year.

If the user entered year is not a century year, then we check if the year is perfectly divisible by 4. If yes, then its a leap year, if not, then its not a leap year.

Check Leap Year Using Ternary Operator: C Program

 
#include < stdio.h >

int main()
{
    int year;

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

    (year % 100 == 0) ?
    ( (year % 400 == 0)?
      (printf("%d is leap year\n", year)):
      (printf("%d is not leap year\n", year))
    ) :
    ( (year % 4 == 0)?
      (printf("%d is leap year\n", year)):
      (printf("%d is not leap year\n", year))
    );

    return 0;
}

Output 1:
Enter the year
2019
2019 is not leap year!

Output 2:
Enter the year
2004
2004 is leap year!

Output 3:
Enter the year
2024
2024 is leap year!

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

You can run the program and input any of the above leap year and check for the output.

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

Programming Interview / Viva Q&A: 5 (while loop)

In this video tutorial lets understand the while loop a little better. We’ve posted source code of a C program below which has while loop. You need to guess the output of the program.

Related Read:
while loop in C programming

Guess output of the program

 

int main()
{
    int count = 0;

    printf("Start of program\n");

    while(count <= 5);
    {
        printf("Count = %d\n", count);
        count = count + 1;
    }

    printf("End of program\n");

    return 0;
}

Output:
Start of program

Since above C source code has semicolon(;) immediately after while(count <= 5) the control enters infinite loop or indefinite loop, and no other statements after the semicolon gets executed. And the program doesn’t get terminated with return 0.

C Programming Interview / Viva Q&A: 5 (While Loop)


[youtube https://www.youtube.com/watch?v=fqYykTU-jrA]

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


If you remove the semicolon(;) after while(count <= 5) then the program will output numbers from 0 to 5 and the “End of program” statement.

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

C Program to Find if a given Year is a Leap Year or Not.

Leap Year Logic

Leap Year
1. If a year is a century year(years 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.

Not Leap Year
1. If the year entered is a century year(perfectly divisible by 100), but not perfectly divisible by 400. Then it’s not a leap year.
2. A year which is not a century year and is not perfectly divisible by 4 is not a leap year.

Leap Year Program: C Programming


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

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


In this video tutorial: inside if block we first check if the entered number is a century year. If yes, then we see if it’s perfectly divisible by 400. If true, then its a leap year. If not, its not a leap year.

If the control shifts to else block then the entered year is not a century year. So we check if the year is perfectly divisible by 4. If yes, then it’s a leap year. If not, it’s not a leap year.

Source Code For Leap Year Program: C Programming

 
#include < stdio.h >

int main()
{
    int year;

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

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

    return 0;
}

Output 1:
Enter the year
2019
2019 is not leap year!

Output 2:
Enter the year
2020
2020 is leap year!

Output 3:
Enter the year
2000
2000 is leap year!

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

You can run the program and input any of the above leap year and check for the output.

This program is also a perfect example for nested if else.

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

Nested if else Statement In C

In this program we’ll show you nesting of if else statements. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.

Simple Logic

When the condition in if statement is false then only else block gets executed. In our program, we check, if percentage is greater than or equal to 60. If it is false, then only control shifts to else block. So inside else(nested if else) we need not once again check if the percentage is less than 60.

Note: We are not using else if and logical operator in this C program purposefully. We want to show nesting of if else in this program.

 
#include < stdio.h >

int main()
{
    float s1, s2, s3, s4, s5, per;

    printf("Enter marks of 5 subject\n");
    scanf("%f %f %f %f %f", &s1, &s2, &s3, &s4, &s5);

    per = (s1 + s2 + s3 + s4 + s5) / 5.0;

    if(per >= 60)
    {
        printf("Grade A\n");
    }
    else
    {
        if(per >= 50)
        {
            printf("Grade B\n");
        }
        else
        {
            if(per >= 40)
            {
                printf("Grade C\n");
            }
            else
            {
                printf("Failed!\n");
            }
        }
    }

    return 0;
}

Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A

Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B

Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C

Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!

Student Grade Calculation using Nested if else: C Program


[youtube https://www.youtube.com/watch?v=I-EHRu-Nq8o]

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


Note: In next video we’ll show you how to perform the same operation as shown in above C program, using else-if and logical operators.

Also check the program C Program To Check Leap Year for a perfect nested if else example.

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

Calculate Area of a Circle without using math.h library: C

We can calculate area of circle if we know the value of its radius. So using that, we shall write the logic in c program to calculate the area of a circle, without using math.h library functions.

Related Read:
Basic Arithmetic Operations In C

 
#include < stdio.h >

int main()
{
    float radius;
    const float PI = 3.14;

    printf("Enter the value for radius \n");
    scanf("%f", &radius);

    printf("Area of the circle is %f\n", (PI * radius * radius));

    return 0;
}

Output:
Enter the value for radius
5.0
Area of the circle is 78.500003

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Calculate Area of a Circle without using math.h library: C


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

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


Whenever we declare and initialize a constant, its a convention to make use of capital letter variable. That way we can easily spot and identify a constant in our program. And as we already know, value of constant can not be changed through out the program execution.
Ex: const float PI = 3.14;

Formula for calculating Area of a circle


area of circle

Formula for calculating Area of a circle is PI * radius * radius. We take value of radius from user and using above formula calculate area of the circle and output result to the console window.

Note that we’ve not used math.h library and any of its methods/functions in our program.

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