C लैंग्वेज में स्विच केस स्टेटमेंट का उपयोग दिए गए विकल्पों में से किसी एक विकल्प को चुनने के लिए किया जाता है | यह एक कण्ट्रोल स्टेटमेंट स्टेटमेंट है जो हमें कई विकल्पों में से चुनने का सुविधा प्रदान करता है |
स्विच में एक expression पास किया जाता है जिसे भिन्न भिन्न केस में दिए गए विकल्पों के साथ तुलना किया जाता है और जिस भी केस विकल्प के साथ स्विच का expression match होता है उसी केस का स्टेटमेंट एक्सीक्यूट होता है यदि कोई भी केस वैल्यू स्विच expression से match नहीं होता है तो डिफ़ॉल्ट स्टेटमेंट एक्सीक्यूट होता है |
syntax:-
switch(expression)
{
case constant-1:
statement-1;
break;
case constant-2:
statement-2;
break;
case constant-3:
statement-3;
break;
-----------
..............
case constant-n:
statement-n;
break;
default :
statement;
}
उपर दिए गए syntax में constant या तो character हो सकता है या integer हो सकता है |
जब स्विच स्टेटमेंट को एक्सीक्यूट किया जाता है तो expression का मूल्यांकन किया जाता है और जिसका केस वैल्यू expression से match होता है कण्ट्रोल को सीधे उस स्टेटमेंट के समूह में ट्रान्सफर किया जाता है |
अगर कोई भी केस वैल्यू expression से match नहीं करता है तो default ब्लाक execute होता है और अगर कोई भी केस वैल्यू expression से match नहीं खता है और साथ में default ब्लाक भी नहीं है तो switch द्वारा कोई भी action नहीं लिया जाता है और control स्विच से बहार ट्रान्सफर हो जाता है |
Flowchart:-
Example:-
#include<stdio.h>#include<conio.h>
void main()
{
int n1,n2;
char oper;
printf("Enter Any Arithmatic Operator\n");
scanf("%c",&oper);
printf("Enter Any Number");
scanf("%d%d",&n1,&n2);
switch(oper)
{
case '+':
printf("%d+%d= %d",n1,n2,n1+n2);
break;
case '-':
printf("%d-%d=%d",n1,n2,n1-n2);
break;
case '*':
printf("%d*%d=%d",n1,n2,n1*n2);
break;
case '/':
printf("%d/%d=%d",n1,n2,n1/n2);
break;
default:
printf("Invalid Operator");
}
getch();
}
OUTPUT:-
Prog:-Write a Menu Driven Program using Switch case for find sum of two number, area of circle and simple interest through user choice
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
Float rd,area;
int n1,n2,n;
int si,p,r,t;
clrscr();
printf("Press 1 for addition of two number\n");
printf("Press 2 for Simple Interest\n");
printf("Press 3 for Area of Circle\n");
switch(choice)
{
case 1:
printf("Enter any two number");
scanf("%d%d",&n1,&n2);
n=n1+n2;
printf("Sum of two number=%d",n);
break;
case 2:
printf("Enter any two number");
scanf("%d%d%d",&p,&r,&t);
si=(p*r*t)/100;
printf("Simple Interest=%d",si);
break;
Case 3:
printf (" Enter Radious");
scanf ("%f",&rd);
area=3.14*rd*rd;
printf ("Area of Circle=%f",area);
Break;
default:
printf("Invalid Entry");
}
getch();
}
Prog 3:- Wriite a program to calculate consonent and vowel for given character using switch case
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
printf("Enter any character\n");
scanf("%c",&c);
switch(c)
{
case 'a':
case 'A' :
printf("Enter character is vowel");
break;
case 'e' :
case 'E' :
printf("Enter character is vowel");
break;
case 'i':
case 'I' :
printf("Enter character is vowel");
break;
case 'o':
case 'O' :
printf("Enter character is vowel");
break;
case 'u':
case 'U' :
printf("Enter character is vowel");
break;
default :
printf("Enter character is consonent");
}
getch();
}






Welcome to OnlineGuru2020.blogspot.com, your number one source for all things related to Online Education. We're dedicated to providing you the very best of Digital Education, with an emphasis on E-notes,Download Links in PDF and youtube videos etc
OnlineGuru2020 Founded in [2020] by Sapan Kumar Das (Asst. Proff Ramchandi College Saraipali), OnlineGuru2020.blogspot.com has come a way from its beginnings .When I first started, my passion inspired me to start my own blog, seeing the need for students in lockdown2020
We hope you enjoy our products as much as we enjoy offering them to you. If you have any questions or comments, please don't hesitate to contact us.
Contact Datails:-
Mobile Number:-9826026747
Email:-sapam.online@gmail.com
Sincerely,
Sapan Kumar Das
.
No comments:
Post a Comment