×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to extract the last two digits from a given year

Here, we are going to learn how to extract the last two digits from a given year using C program?
Submitted by Nidhi, on August 27, 2021

Problem statement

Read the year from the user, and extract the last two digits, and print on the screen.

C program to extract the last two digits from a given year

The source code to extract the last two digits from a given year is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to extract last two digits
// from a given year

#include <stdio.h>

int main()
{
    int year = 0;
    int res = 0;

    printf("Enter year: ");
    scanf("%d", &year);

    res = year % 100;

    printf("Result is: %02d", res);

    return 0;
}

Output

RUN 1:
Enter year: 2021
Result is: 21

RUN 2:
Enter year: 1988
Result is: 88

Explanation

Here, we read the year from the user and extracted the last two digits from the input year using the "%" operator and assigned the result into the res variable, and printed the result on the console screen.

C Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.