C-PROGRAMMING HANDOUT#6

8
C-Programming Walchand Institute of Technology, 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.

Transcript of C-PROGRAMMING HANDOUT#6

Page 1: C-PROGRAMMING HANDOUT#6

C-Programming

Walchand Institute of Technology, 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.

Page 2: C-PROGRAMMING HANDOUT#6

C-Programming

Walchand Institute of Technology, 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 ]

Page 3: C-PROGRAMMING HANDOUT#6

C-Programming

Walchand Institute of Technology, Solapur Page 3

Flow diagram for switch case:

Page 4: C-PROGRAMMING HANDOUT#6

C-Programming

Walchand Institute of Technology, Solapur Page 4

Flowchart for Problem Statement:

Page 5: C-PROGRAMMING HANDOUT#6

C-Programming

Walchand Institute of Technology, 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

Page 6: C-PROGRAMMING HANDOUT#6

C-Programming

Walchand Institute of Technology, 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("\n\n 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);

Page 7: C-PROGRAMMING HANDOUT#6

C-Programming

Walchand Institute of Technology, 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

Page 8: C-PROGRAMMING HANDOUT#6

C-Programming

Walchand Institute of Technology, 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