This Program mainly illustrates the use of 'Switch" control structure. When there are varied number of options and when we have to select 1 option , 'switch' is used.
We first get the values of 2 numbers and enter our choice to add,subtract,multiply or divde the numbers.
Important points to remember while using 'switch':
We first get the values of 2 numbers and enter our choice to add,subtract,multiply or divde the numbers.
Important points to remember while using 'switch':
- The value that is being switched must be an integer [x in this program].
- There must be a space between the 'case' ant the no. [case 1:]
- Every caes must end up with the 'break'.
- 'default' is mandatory. 'break' is not necessary for default.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,x;
float c;
clrscr();
printf("Enter A:");
scanf("%d",&a);
printf("Enter B:");
scanf("%d",&b);
printf("\n");
printf("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n");
printf("Enter your choice:");
scanf("%d",& x);
switch(x)
{
case 1: c=a+b;
printf("The Sum is:%f",c);
break;
case 2: c=a-b;
printf("The difference is:%f", c);
break;
case 3: c=a*b;
printf("The Product is:%f", c);
break;
case 4: c=a/b;
printf("The Quotient is:%f",c);
break;
default: printf ("Invalid option");
}
getch();
}
#include<conio.h>
void main()
{
int a,b,x;
float c;
clrscr();
printf("Enter A:");
scanf("%d",&a);
printf("Enter B:");
scanf("%d",&b);
printf("\n");
printf("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n");
printf("Enter your choice:");
scanf("%d",& x);
switch(x)
{
case 1: c=a+b;
printf("The Sum is:%f",c);
break;
case 2: c=a-b;
printf("The difference is:%f", c);
break;
case 3: c=a*b;
printf("The Product is:%f", c);
break;
case 4: c=a/b;
printf("The Quotient is:%f",c);
break;
default: printf ("Invalid option");
}
getch();
}
No comments:
Post a Comment