C PROGRAM ON DATE TIME AND YEAR
Leap Year Program in C
*
* C program to find leap year using if-else Statement
*/
#include<stdio.h>
void main()
{
int year;
printf("Enter a year \n");
scanf("%d", &year);
if (((year % 400) == 0)||(((year%4)==0)&&(year%100)!=0))
printf("%d is a leap year \n", year);
else
printf("%d is not a leap year \n", year);
}
Program Output
Testcase 1: Here is the runtime output of a C program to find the leap year when the year entered by the user is “2012”.
Enter a year
2012
2012 is a leap year
Testcase 2: In this case, we enter the year “2009” as input to check whether a given year is leap year or not.
Enter a year
2009
2009 is not a leap year
C Program to Extract Last Two Digits of a Given Year
/*
* C Program to Extract Last two Digits of a given Year
*/
#include <stdio.h>
int main()
{
int year, yr;
printf("Enter the year ");
scanf("%d", &year);
yr = year % 100;
printf("Last two digits of year is: %02d", yr);
return 0;
}
Runtime Test Cases
Output:
Enter the year 2012
Last two digits of year is: 12
C Program to Convert Time from 12 Hour to 24 Hour Format
/* C Program for converting 12-hour time format to 24-hour time format. */
#include<stdio.h>
#include<string.h>
int main()
{
int hh, mm, ss;
char a[3];
printf("Enter hours 'hh' \t");
scanf("%d", &hh);
printf("Enter minutes 'mm' \t");
scanf("%d", &mm);
printf("Enter seconds 'ss' \t");
scanf("%d", &ss);
printf("Enter string 'am' or 'pm' \t");
scanf("%s", &a);
/*
* user is allowed to enter time only in 12-hour format
* so that 'hh' cannot be greater than 12.
*/
if(hh <= 12 && mm <= 59 && ss <= 59)
{
if((strcmp(a,"PM") == 0) || (strcmp(a,"pm") == 0)
&& (hh != 0) && (hh != 12))
{
hh = hh + 12;
}
if((strcmp(a,"AM") == 0) || (strcmp(a,"am") == 0) && (hh == 12))
{
hh = 0;
}
printf("The obtained 24-hour format of input is \t");
printf("%02d:%02d:%02d", hh, mm, ss);
printf("\n\n");
}
else
{
printf("Provide the correct inputs.");
}
return 0;
}
Runtime Test Cases
1. Enter hours 'hh' 09
Enter minutes 'mm' 15
Enter seconds 'ss' 55
Enter string 'am' or 'pm' pm
The obtained 24-hour format of input is 21:15:55
2. Enter hours 'hh' 12
Enter minutes 'mm' 00
Enter seconds 'ss' 00
Enter string 'am' or 'pm' am
The obtained 24-hour format of input is 00:00:00
3. Enter hours 'hh' 03
Enter minutes 'mm' 55
Enter seconds 'ss' 50
Enter string 'am' or 'pm' am
The obtained 24-hour format of input is 03:55:50
4. Enter hours 'hh' 23
Enter minutes 'mm' 13
Enter seconds 'ss' 11
Enter string 'am' or 'pm' am
Provide the correct inputs.
C Program to Convert Days into Years, Months and Days
/*
* C program to convert given number of days to a measure of time given
* in years, weeks and days. For example 375 days is equal to 1 year
* 1 week and 3 days (ignore leap year)
*/
#include <stdio.h>
#define DAYSINWEEK 7
void main()
{
int ndays, year, week, days;
printf("Enter the number of days\n");
scanf("%d", &ndays);
year = ndays / 365;
week =(ndays % 365) / DAYSINWEEK;
days =(ndays % 365) % DAYSINWEEK;
printf ("%d is equivalent to %d years, %d weeks and %d daysn",
ndays, year, week, days);
}
Runtime Test Cases
Case:1
Enter the number of days
29
29 is equivalent to 0 years, 4 weeks and 1 days
Case:2
Enter the number of days
1000
1000 is equivalent to 2 years, 38 weeks and 4 days