#include<stdio.h>
main()
{
int a,b,c;
char ch;
printf("Enter two nos");
scanf("%d%d",&a,&b);
printf("Enter the operator for the specific operation");
scanf(" %c",&ch);
switch(ch)
{
case '+':c=a+b;
printf("Addition=%d",c);
break;
case '-':c=a-b;
printf("Subtraction=%d",c);
break;
case '*':c=a*b;
printf("Multiplication=%d",c);
break;
case '/':c=a/b;
printf("Division=%d",c);
break;
default:printf("Invalid Character");
}
}
ASSIGNMENT 3)
TO DEMONSTRATE DECISION MAKING STATEMENTS (SWITCH CASE)
You should read following topics before starting this exercise
1. Different types of decision-making statements available in C.
2. Syntax for switch case statements.
The control statements that allows us to make a decision from the number of choices is called a switch-case statement. It is a multi-way decision making statement.
i) Usage of switch statement
Syntax: switch(expression)
{
case value1: block1;
break;
case value2: block2;
break;
main()
{
int a,b,c;
char ch;
printf("Enter two nos");
scanf("%d%d",&a,&b);
printf("Enter the operator for the specific operation");
scanf(" %c",&ch);
switch(ch)
{
case '+':c=a+b;
printf("Addition=%d",c);
break;
case '-':c=a-b;
printf("Subtraction=%d",c);
break;
case '*':c=a*b;
printf("Multiplication=%d",c);
break;
case '/':c=a/b;
printf("Division=%d",c);
break;
default:printf("Invalid Character");
}
}
ASSIGNMENT 3)
TO DEMONSTRATE DECISION MAKING STATEMENTS (SWITCH CASE)
You should read following topics before starting this exercise
1. Different types of decision-making statements available in C.
2. Syntax for switch case statements.
The control statements that allows us to make a decision from the number of choices is called a switch-case statement. It is a multi-way decision making statement.
i) Usage of switch statement
Syntax: switch(expression)
{
case value1: block1;
break;
case value2: block2;
break;
.
.
default: default block;
break;
}
ii) The switch statement is used in writing menu driven programs here a menu displays several options and the user gives the choice by typing a character or number .
No comments:
Post a Comment