switch case program number in word in c Guide

switch case program number in word in c एक ऐसा बेसिक लेकिन बहुत ही महत्वपूर्ण program है, जिसके माध्यम से आप C language में decision making को बेहतर तरीके से समझ सकते हैं। इस Program में User से 1 से 5 के बीच कोई भी संख्या ली जाती है और उस संख्या को उसके word format (जैसे One, Two, Three) में Print किया जाता है।

इस Program को समझने से आप को यह फायदा होगा कि वे switch case की working, उसका structure, और multiple conditions को handle करने का सही तरीका सीख पाएंगे। यह खासकर beginners के लिए बहुत उपयोगी है क्योंकि इससे if-else की तुलना में switch statement की clarity समझ में आती है। साथ ही, input लेना, condition check करना और output display करना – ये तीनों concepts एक साथ clear हो जाते हैं।

Program statement

एक C प्रोग्राम लिखिए जो यूज़र से 1 से 5 के बीच कोई संख्या इनपुट ले और switch case का उपयोग करके उस संख्या को word में प्रिंट करे। यदि यूज़र 1 से 5 के बाहर कोई संख्या दर्ज करता है, तो “Invalid number! Please enter a number between 1 and 5.” मैसेज दिखाए।

Program Code

#include<stdio.h>
int main()
{
int num;
printf("Enter a number (1 to 5) : ");
scanf("%d",&num);
switch(num)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
case 4:
printf("Four");
break;
case 5:
printf("Five");
break;
default:
printf("Invalid number! Please enter a number between 1 and 5.\n");
}
return 0;
}

Program output :

Example 1:
Enter a number (1 to 5) : 3
Three

Example 2:
Enter a number (1 to 5) : 7
Invalid number! Please enter a number between 1 and 5.

Code Explanation

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

int main() program का entry point होता है, जहां से execution शुरू होता है।

int num; के माध्यम से एक integer variable declare किया गया है, जिसमें यूज़र द्वारा दर्ज की गई संख्या store होगी।

printf() function यूज़र को message दिखाता है कि उसे 1 से 5 के बीच कोई संख्या दर्ज करनी है।

scanf(“%d”,&num); यूज़र का input लेता है और उसे num variable में store करता है।

इसके बाद switch(num) statement शुरू होता है। यह num की value को check करता है और जिस case से match होता है, वही block execute करता है।

  • अगर num = 1 है, तो “One” प्रिंट होगा।
  • अगर num = 2 है, तो “Two” प्रिंट होगा।
  • इसी तरह 3, 4 और 5 के लिए अलग-अलग case define किए गए हैं।

हर case के बाद break; लगाया गया है ताकि एक case execute होने के बाद program switch block से बाहर निकल जाए।

अगर user 1 से 5 के अलावा कोई संख्या डालता है, तो default case execute होगा और invalid message दिखाया जाएगा।

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

यह प्रोग्राम beginners के लिए switch statement को practically समझने का एक बेहतरीन उदाहरण है।

Leave a Comment

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

Scroll to Top