हिंदी माध्यम नोट्स
C++ : Switch Statement ( उदाहरण) , write a program to display day of week , even – odd number using switch statement
write a program to display day of week .
इस उदाहरन मे , week के day का नाम को display किया जाता है |
Explanation
सबसे पहले यूजर द्वारा day number को discuss किया जाता है |
अगर यूजर द्वारा input किये गये day number निन्म होते है तब
अगर input = 1 होता है तब monday display होगा |
अगर input = 2 होता है तब tuseday display होगा |
अगर input = 3 होता है तब wednesday display होगा |
अगर input = 4 होता है तब thrsday display होगा |
अगर input = 5 होता है तब friday display होगा |
अगर input = 6 होता है तब saturday display होगा |
अगर input = 7 होता है तब sunday display होगा |
default case मे invalid week number का message display होता हो |
switch statement मे case lable मे यूजर द्वारा input किये गये week number को लिखा जाता है | और उसके अनुसार day name को display किया जाता है |
source code
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int week_number ;
cout<<“Enter week number(from 1-7) : “;
cin>>week_number
switch(week_number)
{
case ‘1’ :
cout<<“Your day name Monday “;
break;
case ‘2’ :
cout<<“Your day name tuseday “;
break;
case ‘3’ :
cout<<“Your day name wednesday “;
break;
case ‘4’ :
cout<<“Your day name thrsday “;
break;
case ‘5’ :
cout<<“Your day name friday “;
break;
case ‘6’ :
cout<<“Your day name saturday “;
break;
case ‘7’ :
cout<<“Your day name sunday “;
break;
defalut :
cout<<“Invalid week number “;
break;
}
getch();
}
इसका आउटपुट होगा
Enter week number(from 1-7) : 7
Your day name sunday
Example 2
write a program to find even – odd number using switch statement .
इस उदाहरन मे , दिए गये number को even – odd number को find किया जाता है |
Exaplanation
सबसे पहले यूजर द्वारा number को declare किया जाता है | उसके बाद यूजर द्वारा number को input किया जाता है | इस value को number मे assign कर दिया जाता है |
switch statement मे number को 2 से divid किया जाता है | और इसके remainder को ‘0’ से check किया जाता है | इस expression से दो आउटपुट मिलता है |
अगर case ‘1’ होता है तब number is even display होगा |
और अगर case ‘0’ होता है तब number is odd display होगा
default case मे invalid choice का message display होता हो |
switch statement मे case lable मे यूजर द्वारा input किये गये expression को लिखा जाता है | और उसके अनुसार message को display किया जाता है |
source code
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int number ;
cout<<“Enter number : “;
cin>>number
switch((number%2)==0)
{
case ‘1’ :
cout<<“number is even “;
break;
case ‘0’ :
cout<<“number is odd “;
break;
defalut :
cout<<“Invalid number “;
break;
}
getch();
}
इसका आउटपुट होगा :
Enter number : 12
number is even
Example 3
write a program to find maximum number using switch statement .
इस उदाहरन मे , दिए गये number को maximum number को find किया जाता है |
Exaplanation
सबसे पहले यूजर द्वारा दो variable को declare किया जाता है | उसके बाद यूजर द्वारा numbers को input किया जाता है | इस value को variable मे assign कर दिया जाता है |
switch statement मे num 1 < num2 expression होता है | इस statement से दो आउटपुट होता है |
अगर case ‘1’ होता है तब maximum number : num 1 display होगा |
और अगर case ‘0’ होता है तब maximum number : num 2 display होगा
default case मे both numbers are equal. का message display होता हो |
switch statement मे case lable मे यूजर द्वारा input किये गये expression को लिखा जाता है | और उसके अनुसार message को display किया जाता है |
source code
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int num1 , num2 ;
cout<<“Enter number1 : “;
cin>>num1;
cout<<“Enter number2 : “;
cin>>num2;
switch(num1 > num2 )
{
case ‘1’ :
cout<<“maximum number : “<<num1;
break;
case ‘0’ :
cout<<“maximum number : “<<num2;
break;
defalut :
cout<<“both numbers are equal. “;
break;
}
getch();
}
इसका आउटपुट होगा :
Enter number1 : 12
Enter number2 : 23
maximum number :23
Example 4
write a program to find vowel character using switch statement .
इस उदाहरन मे , दिए गये number को vowel character को find किया जाता है |
Exaplanation
सबसे पहले यूजर द्वारा character को declare किया जाता है | उसके बाद यूजर द्वारा character को input किया जाता है | इस value को character variable मे assign कर दिया जाता है |
switch statement मे यूजर द्वारा input character को लिखा जाता है | vowels को check करने के लिए निन्म case value होती है |
अगर case ‘a’ होता है तब character is vowel. display होगा |
और अगर case ‘e’ होता है तब character is vowel. display होगा |
अगर case ‘i’ होता है तब character is vowel. display होगा |
और अगर case ‘o’ होता है तब character is vowel. display होगा |
अगर case ‘u’ होता है तब character is vowel. display होगा |
और अगर case ‘A’ होता है तब character is vowel. display होगा |
अगर case ‘E’ होता है तब character is vowel. display होगा |
और अगर case ‘O’ होता है तब character is vowel. display होगा |
अगर case ‘U’ होता है तब character is vowel. display होगा |
और अगर case ‘I’ होता है तब character is vowel. display होगा |
default case मे invalid choice का message display होता हो |
switch statement मे case lable मे यूजर द्वारा input किये गये expression को लिखा जाता है | और उसके अनुसार message को display किया जाता है |
source code
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char character ;
cout<<“Enter character : “;
cin>>character;
switch(character)
{
case ‘a’ :
cout<<“character is vowel.”;
break;
case ‘e’ :
cout<<“character is vowel. “;
break;
case ‘i’ :
cout<<“character is vowel.”;
break;
case ‘o’ :
cout<<“character is vowel. “;
break;
case ‘u’ :
cout<<“character is vowel.”;
break;
case ‘A’ :
cout<<“character is vowel. “;
break;
case ‘E’ :
cout<<“character is vowel.”;
break;
case ‘O’ :
cout<<“character is vowel. “;
case ‘U’ :
cout<<“character is vowel.”;
break;
case ‘I’ :
cout<<“character is vowel. “;
break;
defalut :
cout<<“character is constnat “;
break;
}
getch();
}
इसका आउटपुट होगा :
Enter character : a
character is vowel.
इस article मे switch statement पर based चार उदाहरनो को discuss किया जाता है | जिससे प्रोग्रामर के switch statement के concept को अच्छी तरह से समज सकता है |
Recent Posts
Question Tag Definition in english with examples upsc ssc ias state pcs exames important topic
Question Tag Definition • A question tag is a small question at the end of a…
Translation in english grammer in hindi examples Step of Translation (अनुवाद के चरण)
Translation 1. Step of Translation (अनुवाद के चरण) • मूल वाक्य का पता करना और उसकी…
Report Writing examples in english grammer How to Write Reports explain Exercise
Report Writing • How to Write Reports • Just as no definite rules can be laid down…
Letter writing ,types and their examples in english grammer upsc state pcs class 12 10th
Letter writing • Introduction • Letter writing is an intricate task as it demands meticulous attention, still…
विश्व के महाद्वीप की भौगोलिक विशेषताएँ continents of the world and their countries in hindi features
continents of the world and their countries in hindi features विश्व के महाद्वीप की भौगोलिक…
भारत के वन्य जीव राष्ट्रीय उद्यान list in hin hindi IAS UPSC
भारत के वन्य जीव भारत में जलवायु की दृष्टि से काफी विविधता पाई जाती है,…