Switch Case Program Month in C Explained

switch case program month in c एक बहुत ही उपयोगी और beginner-friendly program है, जिसकी मदद से आप 1 से 12 तक के month number को उसके संबंधित महीने के नाम में बदलना सीखेंगे। इस program के माध्यम से आप यह समझ पाएंगे कि C language में switch case कैसे काम करता है, user input कैसे लिया जाता है, और multiple conditions को efficiently कैसे handle किया जाता है।

इस program को सीखने का सबसे बड़ा फायदा यह है कि आप decision-making structure को अच्छे से समझ पाएंगे। यह logic आगे चलकर menu-driven programs, calculator, ATM simulation और कई practical applications में काम आता है।

Program statement

एक C प्रोग्राम लिखिए जो user से 1 से 12 तक का month number इनपुट के रूप में ले और उसके अनुसार महीने का नाम print करे। यदि user 1 से 12 के अलावा कोई अन्य संख्या दर्ज करता है, तो “Invalid month number!” संदेश दिखाए।

Program Code

#include <stdio.h>
int main()
{
int month_number;
printf("Enter month number (1 To 12) : ");
scanf("%d", &month_number);
switch (month_number)
{
case 1:
printf("Month is January");
break;
case 2:
printf("Month is February");
break;
case 3:
printf("Month is March");
break;
case 4:
printf("Month is April");
break;
case 5:
printf("Month is May");
break;
case 6:
printf("Month is June");
break;
case 7:
printf("Month is July");
break;
case 8:
printf("Month is August");
break;
case 9:
printf("Month is September");
break;
case 10:
printf("Month is October");
break;
case 11:
printf("Month is November");
break;
case 12:
printf("Month is December");
break;
default:
printf("Invalid month number!");
}
return 0;
}

Program output :

Enter month number (1 To 12) : 4

Month is April

अगर user 15 input करता है तो output होगा:

Invalid month number!

Code Explanation

सबसे पहले #include <stdio.h> header file को include किया गया है, जिससे input और output functions जैसे printf() और scanf() का उपयोग किया जा सके।

int main() program का starting point है। इसके अंदर int month_number; नाम का एक integer variable declare किया गया है, जिसमें user द्वारा डाला गया month number store होगा।

printf() function user से input मांगने के लिए message display करता है।
scanf(“%d”, &month_number); user का input लेकर month_number variable में store करता है।

इसके बाद switch (month_number) statement शुरू होता है। यहाँ switch condition check करता है कि user ने कौन-सा number enter किया है।

हर case एक specific number को represent करता है।
जैसे:

  • case 1 → January
  • case 2 → February
  • इसी तरह case 12 → December

हर case के बाद break; लिखा गया है ताकि सही case execute होने के बाद program switch block से बाहर निकल जाए। अगर break नहीं होगा तो program नीचे वाले cases भी execute कर देगा, जिसे fall-through कहते हैं।

default case तब execute होता है जब user 1 से 12 के अलावा कोई अन्य number input करता है। इससे program invalid input को handle करता है।

अंत में return 0; program के successful execution को दर्शाता है।

यह program beginners के लिए switch case की working समझने का बेहतरीन उदाहरण है।

Leave a Comment

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

Scroll to Top