Time Based Greeting Message Display Program In C

आज आप time based greeting message display program In c बनाना सीखेंगे। program के साथ साथ step-by-step explanation भी देखेंगे । 

Program Statement

C में एक program लिखिये जो user से time input ले (0–23)।
अगर:

  • time < 0 या time > 23 →  display  “Invalid Time Input”
  • 5 –11 → Good Morning
  • 12–16 → Good Afternoon
  • 17–20 → Good Evening
  • 21–23 और 0–4 → Good Night

अब चलिये Program को समझते हैं।

Program Code 

#include <stdio.h>

int main()
{
    int time;

    printf("Enter Time (0 - 23): ");
    scanf("%d", &time);

    if (time < 0 || time > 23)
    {
        printf("\nInvalid Time Input!");
    }
    else if (time >= 5 && time <= 11)
    {
        printf("\nGood Morning");
    }
    else if (time >= 12 && time <= 16)
    {
        printf("\nGood Afternoon");
    }
    else if (time >= 17 && time <= 20)
    {
        printf("\nGood Evening");
    }
    else
    {
        printf("\nGood Night");
    }

    return 0;
}

Output:

Enter Time (0 – 23): 10

Good Morning

Program Explanation

1: Header File

#include <stdio.h>

यह standard input-output library है।
printf() और scanf() function use करने के लिये किया गया हैं।

2: Variable Declaration

int time;

यह integer variable user द्वारा दिया गया time (0–23) store करने के लिए है।

3: User Input

printf(“Enter Time (0 – 23): “);

scanf(“%d”, &time);

  • printf() → message display करता है
  • scanf() → user input को variable में store करता है
  • &time → memory address देता है

4: Invalid Time Check

if (time < 0 || time > 23)

अगर time 0 से कम या 23 से ज्यादा है
तो invalid input माना जाएगा।

Logical OR operator || का मतलब है — कोई एक condition true हो तो पूरा condition true।

5: Good Morning

else if (time >= 5 && time <= 11)

अगर time 5 से 11 के बीच है
तो “Good Morning” display होगा।

&& operator का मतलब है — दोनों condition true होनी चाहिए।

6: Good Afternoon

else if (time >= 12 && time <= 16)

Time 12 से 16 के बीच होने पर
“Good Afternoon” display होगा।

7: Good Evening

else if (time >= 17 && time <= 20)

Time 17 से 20 के बीच होने पर
“Good Evening” display होगा।

8: Good Night

बाकी सभी valid cases (21–23 और 0–4) में
“Good Night” display होगा।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top