C Program To Determine Leap Year or Not using Macros

In this video tutorial lets learn how to determine user input year is a leap year or not, using Macros and nested ternary / conditional operator.

Related Read:
C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator
Using Macro Template In Macro Expansion: C Program

Leap Year Logic

1. If a year is century year(year ending with 00) and is perfectly divisible by 400, then it’s a leap year.

2. If a year is not a century year, and is perfectly divisible by 4, then it’s a leap year.

Video Tutorial: Determine Leap Year or Not using Macros: C Program


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

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

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

#include<stdio.h>

#define NOT_LEAP(x) printf("%d is not leap year\n", x)
#define LEAP_YEAR(x) printf("%d is leap year\n", x)

#define LEAP(x) ( (x % 100 == 0 && x % 400 == 0) ? LEAP_YEAR(x) : \
                 ( (x % 4 ==0) ? LEAP_YEAR(x) : NOT_LEAP(x)) )

int main()
{
    int year;

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

    LEAP(year);

    return 0;
}

Output 1:
Enter a year
2018
2018 is not leap year

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

Output 3:
Enter a year
2020
2020 is leap year

Output 4:
Enter a year
2023
2023 is not leap year

Output 5:
Enter a year
2024
2024 is leap year

In above program we’re writing NOT_LEAP(x) macro to display a message that the user input year is not a leap year. LEAP_YEAR(x) macro is used to display message that the user input year is a leap year.

We use both NOT_LEAP(x) and LEAP_YEAR(x) macro names or macro templates inside LEAP(x) macro expansion.

We’re also using macro continuation(\) to break the line and continue writing the logic in next line in macro expansion of LEAP(x).

List of some leap years

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

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

Swap 2 Numbers Using Macros: C Program

Today lets learn how to swap two integer numbers(using a temporary variable) using Macros in C.

Video Tutorial: Swap 2 Numbers Using Macros: C Program


[youtube https://www.youtube.com/watch?v=esQ-DmyPYYc]

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

Source Code: Swap 2 Numbers Using Macros: C Program

#include<stdio.h>

#define SWAP(x, y, temp) temp = x; x = y; y = temp;

int main()
{
    int a, b, temp;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &a, &b);

    printf("Before swapping: a = %d and b = %d\n", a, b);

    SWAP(a, b, temp);

    printf("After swapping: a = %d and b = %d\n", a, b);

    return 0;
}

Output:
Enter 2 integer numbers
20
50
Before swapping: a = 20 and b = 50
After swapping: a = 50 and b = 20

Logic To Swap Two Numbers

First value of a is transferred to temp;
Next value of b is transferred to a.
Next value of temp is transferred to b.



That’s how value of a and b are swapped using a temporary variable.

Note: Preprocessor replaces the macro template(SWAP(a, b, temp)) with its corresponding macro expansion(temp = x; x = y; y = temp;) before passing the source code to the compiler.

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

Calculate Sum of Digits: C Program

Lets ask the user to input a integer number through keyboard. Lets write a c program to calculate sum of its digits.

Related Read:
Find Sum of Digits In A Given Number: C

Note: We assign variable sum = 0 to avoid garbage values in sum.

If user enters 456, we apply the modulo division to get the individual values.
Ex:
456 % 10 = 6
45 % 10 = 5
4 % 10 = 4

We get 456, 45 and 4 by dividing the original value by 10.
Ex:
456 user entered value.
456 / 10 = 45
45 / 10 = 4

Now the sum.

sum = sum + rem;

06 = 0 + 6
11 = 6 + 5
15 = 11 + 4

So the sum of digits in the given integer number is 15.

Calculate Sum of Digits: C Program


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

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


Source Code: Calculate Sum of Digits: C Program

#include < stdio.h >

int main()
{
    int num, reminder, sum = 0;

    printf("Enter a integer number\n");
    scanf("%d", &num);

    while(num != 0)
    {
     reminder = num % 10;
     sum      = sum + reminder;
     num      = num / 10;
    }

    printf("Sum of digit is %d\n", sum);

    return 0;
}

Output 1:
Enter a integer number
456
Sum of digit is 15

Output 2:
Enter a integer number
8910
Sum of digit is 18

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

Convert Kilometer To Meter, Centimeter, Millimeter: C Program

The distance between two cities (in Kilometer) is input through the keyboard. Write a program to convert and print this distance in meters, centimeter, millimeter.

Note:
1 Kilometer(km) = 1000 Meters(m).
1 Kilometer(km) = 100000 Centimeters(cm).
1 Kilometer(km) = 1000000 Millimeters(mm).

For n Kilometers(KM)
n Kilometer = n * 1000 Meters
n Kilometer = n * 100000 Centimeters
n Kilometer = n * 1000000 Millimeters

Convert Kilometer To Meter, Centimeter, Millimeter: C Program


[youtube https://www.youtube.com/watch?v=zj79Yj0-ctI]

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


Source Code: Convert Kilometer To Meter, Centimeter, Millimeter: C Program

#include < stdio.h >

int main()
{
    float km, cm, mm, m;

    printf("Enter distance in Kelometer\n");
    scanf("%f", &km);

    m  = km * 1000.0;
    cm = km * 100000.0;
    mm = km * 1000000.0;

    printf("Distance in Meter is %f\n", m);
    printf("Distance in Centimeter is %f\n", cm);
    printf("Distance in Milimeter is %f\n", mm);

    return 0;
}

Output 1:
Enter distance in Kelometer
10
Distance in Meter is 10000.000000
Distance in Centimeter is 1000000.000000
Distance in Milimeter is 10000000.000000

Output 2:
Enter distance in Kelometer
5
Distance in Meter is 5000.000000
Distance in Centimeter is 500000.000000
Distance in Milimeter is 5000000.000000

PrefixSymbolMultiply by
yottaY1024
zettaZ1021
exaE1018
petaP1015
teraT1012
gigaG109
megaM106
hectokilohk105
myriama104
kilok103
hectoh102
dekada101
UNIT1100
decid10-1
centic10-2
millim10-3
decimillidm10-4
centimillicm10-5
microµ10-6
nanon10-9
picop10-12
femtof10-15
attoa10-18
zeptoz10-21
yoctoy10-24

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

Area of Rectangle: C Program

The length and breadth / width of a rectangle are entered through the keyboard. Write a program to calculate the area of the rectangle.

Rectangle is a quadrilateral(i.e., it has 4 sides). And all the 4 angles of the rectangle are of 90 degree. To calculate area of a rectangle we need its length and width/breadth.

Related Read:
Basic Arithmetic Operations In C
Perimeter of Rectangle: C Program

Note: Area of a Rectangle is calculated using the formula
Area = Length * Width;


area of rectangle

Length: is the length of the longest side of the rectangle.
width: is the length of the smallest side of the rectangle.

Find Area of a Rectangle: C Program


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

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


Source Code: Find Area of a Rectangle: C Program

#include < stdio.h >

int main()
{
    float length, width, area;

    printf("Enter length of rectangle\n");
    scanf("%f", &length);

    printf("Enter width of rectangle\n");
    scanf("%f", &width);

    area = length * width;

    printf("Area of Rectangle is %f\n", area);

    return 0;
}

Output:
Enter length of rectangle
12
Enter width of rectangle
6
Area of Rectangle is 72.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