JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

C++ : Switch Statement ( उदाहरण) , write a program to display day of week , even – odd number using switch statement

इससे पहले के article मे switch statement को discuss किया है  अगर multiple condition को check करना होता है तब switch statement को use किया जाता है | इस article मे  कुछ उदाहरनो को discuss करेगे जिससे switch statement को अच्छी  तरह से समजा जा सके  |Example 1
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 को अच्छी तरह से समज सकता है |

Sbistudy

Recent Posts

सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है

सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…

10 hours ago

मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the

marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…

10 hours ago

राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi

sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…

2 days ago

गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi

gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…

2 days ago

Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन

वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…

3 months ago

polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten

get all types and chapters polity notes pdf in hindi for upsc , SSC ,…

3 months ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now