SlideShare a Scribd company logo
1 of 8
Download to read offline
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 1
Handout#6
Assignment/Program Statement:
Write a C program using ‘switch-case’ statement - to read two values from user
and perform addition, subtraction, multiplication, division and modulus division
Learning Objectives:
Students will be able to
- write the syntax of switch-case statement
- draw the flowchart for switch-case menu driven solution
- write the algorithm for switch-case menu driven
- write the program using switch-case statement
Theory:
 A switch statement allows a variable to be tested for equality against a list
of values. Each value is called a case, and the variable being switched on is
checked for each switch case.
 The syntax for a switch statement in C programming language is as follows-
switch (n)
{
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
 When a case constant is found that matches the switch expression, control of
the program passes to the block of code associated with that case.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 2
 In the above pseudo code, suppose the value of n is equal to constant2. The
compiler will execute the block of code associate with the case statement
until the end of switch block, or until the break statement is encountered.
 The break statement is used to prevent the code running into the next case.
Example: C code to perform addition, subtraction, multiplication and division
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber-secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber/firstNumber);
break;
default:
printf("Error! operator is not correct");
}
[Reference: http://www.programiz.com/c-programming/c-switch-case-statement ]
Flow diagram for switch case:
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 3
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 4
Flowchart for Problem Statement:
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 5
Algorithm:
1. Start
2. Read operand 1 and operand 2
3. Read choice
4. If choice = 1
answer = operand 1 + operand 2
display the answer
go to 5
else
answer = operand 1 - operand 2
display the answer
go to 5
else
answer = operand 1 * operand 2
display the answer
go to 5
else
answer = operand 1 / operand 2
display the answer
go to 5
else
answer = operand 1 % operand 2
display the answer
go to 5
else
display the answer
go to 5
5. Stop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 6
Program:
#include<stdio.h>
void main()
{
int op1, op2, ans, choice;
clrscr();
/*Code to read input operand1 and operand2*/
printf("n Enter First Number:");
scanf("%d", &op1);
printf("n Enter Second Number:");
scanf("%d", &op2);
printf("nn Menu");
printf("n 1. Addition");
printf("n 2. Subtraction");
printf("n 3. Multiplication");
printf("n 4. Division");
printf("n 5. Modulus Division");
printf("n 6. Exit");
printf("n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: ans = op1+op2;
printf("n Addition = %d",ans);
break;
case 2: ans = op1-op2;
printf("n Subtraction = %d",ans);
break;
case 3: ans = op1*op2;
printf("n Multiplication = %d", ans);
break;
case 4: ans = op1/op2;
printf("n Division = %d",ans);
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 7
break;
case 5: ans = op1%op2;
printf("n Modulus operator = %d",ans);
break;
` default: printf("n Wrong choice...");
}
getch();
}
Input:
Enter First Number: 12
Enter Second Number: 12
Menu
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus Division
6. Exit
Enter your choice:1
Output:
Addition = 12
Practice Problem Statement:
1. Write a program to take an integer from user and perform following operations
a) Check whether number is even or odd
b) Check whether number is prime or not
c) Calculate its factorial
d) Check whether number is positive or negative
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 8
Conclusion:
Thus a C program using ‘switch-case’ statement - to read two values from user and
perform addition, subtraction, multiplication, division and modulus division is
implemented.
Learning Outcomes:
At the end of this assignment, students are able to
- write the syntax of switch-case statement
- draw the flowchart for switch-case menu driven solution
- write the algorithm for switch-case menu driven
- write the program using switch-case statement

More Related Content

What's hot (20)

Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
C Programming
C ProgrammingC Programming
C Programming
 
C Programming
C ProgrammingC Programming
C Programming
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
C Programming
C ProgrammingC Programming
C Programming
 
Chowtodoprogram solutions
Chowtodoprogram solutionsChowtodoprogram solutions
Chowtodoprogram solutions
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
C programming
C programmingC programming
C programming
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 

Similar to CP Handout#6

computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)jakejakejake2
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
 

Similar to CP Handout#6 (20)

computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Fundamentals of programming)
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Presentation1
Presentation1Presentation1
Presentation1
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
C PROGRAMMING p-3.pptx
C PROGRAMMING p-3.pptxC PROGRAMMING p-3.pptx
C PROGRAMMING p-3.pptx
 
C programs
C programsC programs
C programs
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
What is c
What is cWhat is c
What is c
 

More from trupti1976

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3trupti1976
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10trupti1976
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9trupti1976
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8trupti1976
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7trupti1976
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6trupti1976
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5trupti1976
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4trupti1976
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2trupti1976
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1trupti1976
 

More from trupti1976 (11)

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1
 
CP Handout#1
CP Handout#1CP Handout#1
CP Handout#1
 

Recently uploaded

Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 

Recently uploaded (20)

Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 

CP Handout#6

  • 1. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 1 Handout#6 Assignment/Program Statement: Write a C program using ‘switch-case’ statement - to read two values from user and perform addition, subtraction, multiplication, division and modulus division Learning Objectives: Students will be able to - write the syntax of switch-case statement - draw the flowchart for switch-case menu driven solution - write the algorithm for switch-case menu driven - write the program using switch-case statement Theory:  A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.  The syntax for a switch statement in C programming language is as follows- switch (n) { case constant1: // code to be executed if n is equal to constant1; break; case constant2: // code to be executed if n is equal to constant2; break; . . . default: // code to be executed if n doesn't match any constant }  When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.
  • 2. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 2  In the above pseudo code, suppose the value of n is equal to constant2. The compiler will execute the block of code associate with the case statement until the end of switch block, or until the break statement is encountered.  The break statement is used to prevent the code running into the next case. Example: C code to perform addition, subtraction, multiplication and division switch(operator) { case '+': printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber); break; case '-': printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber); break; case '*': printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber); break; case '/': printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber); break; default: printf("Error! operator is not correct"); } [Reference: http://www.programiz.com/c-programming/c-switch-case-statement ] Flow diagram for switch case:
  • 3. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 3
  • 4. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 4 Flowchart for Problem Statement:
  • 5. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 5 Algorithm: 1. Start 2. Read operand 1 and operand 2 3. Read choice 4. If choice = 1 answer = operand 1 + operand 2 display the answer go to 5 else answer = operand 1 - operand 2 display the answer go to 5 else answer = operand 1 * operand 2 display the answer go to 5 else answer = operand 1 / operand 2 display the answer go to 5 else answer = operand 1 % operand 2 display the answer go to 5 else display the answer go to 5 5. Stop
  • 6. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 6 Program: #include<stdio.h> void main() { int op1, op2, ans, choice; clrscr(); /*Code to read input operand1 and operand2*/ printf("n Enter First Number:"); scanf("%d", &op1); printf("n Enter Second Number:"); scanf("%d", &op2); printf("nn Menu"); printf("n 1. Addition"); printf("n 2. Subtraction"); printf("n 3. Multiplication"); printf("n 4. Division"); printf("n 5. Modulus Division"); printf("n 6. Exit"); printf("n Enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: ans = op1+op2; printf("n Addition = %d",ans); break; case 2: ans = op1-op2; printf("n Subtraction = %d",ans); break; case 3: ans = op1*op2; printf("n Multiplication = %d", ans); break; case 4: ans = op1/op2; printf("n Division = %d",ans);
  • 7. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 7 break; case 5: ans = op1%op2; printf("n Modulus operator = %d",ans); break; ` default: printf("n Wrong choice..."); } getch(); } Input: Enter First Number: 12 Enter Second Number: 12 Menu 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulus Division 6. Exit Enter your choice:1 Output: Addition = 12 Practice Problem Statement: 1. Write a program to take an integer from user and perform following operations a) Check whether number is even or odd b) Check whether number is prime or not c) Calculate its factorial d) Check whether number is positive or negative
  • 8. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 8 Conclusion: Thus a C program using ‘switch-case’ statement - to read two values from user and perform addition, subtraction, multiplication, division and modulus division is implemented. Learning Outcomes: At the end of this assignment, students are able to - write the syntax of switch-case statement - draw the flowchart for switch-case menu driven solution - write the algorithm for switch-case menu driven - write the program using switch-case statement