C Program to Print Good Morning, Evening, Night according to Time

In this tutorial, we will learn about how to create a program in C that takes time (on 24-hour format) as input from user (at run-time) and prints message accordingly as defined below:

  • If time is greater than 0 and less than or equal to 3, then print Good Night
  • If time is greater than 3 and less than 12, then print Good Morning
  • If time is equal to 12, then print Good Noon
  • If time is greater than 12 and less than or equal to 15, print Good AfterNoon
  • If time is greater than 15 and less than 20, print Good Evening
  • If time is greater than or equal to 20 and less than or equal to 24, print Good Night

In 24-hour time format:

  • If time equals 1 means it is 1 A.M.
  • If time equals 13 means it is 1 P.M.
  • If time equals 15 means it is 3 P.M.
  • If time equals 20 means it is 8 P.M.
  • and so no

Let's take a look at the program:

int main()
    int t;
    printf("Enter the time (1-24): ");
    scanf("%d", &t);
    if(t>0 && t<=3)
        printf("Good Night");
    else if(t>3 && t<12)
        printf("Good Morning");
    else if(t==12)
        printf("Good Noon");
    else if(t>12 && t<=15)
        printf("Good AfterNoon");
    else if(t>15 && t<20)
        printf("Good Evening");
    else if(t>=20 && t<=24)
        printf("Good Night");
        printf("Unknown time!");
    return 0;

As the program was written under Code::Blocks IDE, therefore after successful build and run, here is the snapshot of the sample run:

c program print message with time

Now enter the time say 8. User has to enter the time in 24-hour format, entering 8 means 8 A.M. and 20 means 8 P.M. Here is the second snapshot of the sample run:

print good morning night c program

Now let's check with another sample run by supplying 20 as input. In 24-hour format 20 will be 8PM. Here is the snapshot of the second sample run:

print message with time c program

Below are some of the main steps used in above program:

  • Receive time (a number between 1-24) as input
  • Use if-else statement to check and print the message accordingly
  • For example, if user enters 13 as input, then 13 is greater than 12 and less than or equal to 15, therefore the 4th condition evaluates to be true, and the message Good AfterNoon gets printed on output screen
  • If user enters any time that is not in between 1-24, then the program will output Unknown time!

Print Current Local Time and Date

If you want to print the current local time and date along with day, month and year, in C language, then use the following program. Here we have used pre-defined function and header files available in C to find out the current local date and time for you

int main()
    time_t rtm;
    struct tm *tinfo;
    tinfo = localtime(&rtm);
    printf("Current Local Time/Date: %s", asctime(tinfo));
    return 0;

Here is the sample run of the above program:

print current date time c

C Online Test

« Previous Program Next Program »

Like/Share Us on Facebook 😋