A Switch Case Program in C and C++

A Switch Case Program in C and C++

A Switch Case Program in C and C++

Switch case comes under selection looping structure where the program has to select one option out of several other options. Switch case is an alternative to if else structure. Many a times a program has to check multiple conditions, in such cases if else structure makes program very lengthy and difficult to understand. In such cases switch case plays an important role. It breaks multiple conditions in cases and checks them one by one. Let’s see a switch case program in C and C++.

1. Code

C program

//A Switch Case Program in C and C++
#include<stdio.h>

int main()
{
	int a,b,opr;
	printf("\nEnter two numbers:");
	scanf("%d%d",&a,&b);
	printf("\nSelect the operation to perform.");
	printf("\n1.Addition");
	printf("\n2.Subtraction");
	printf("\n3.Multiplication");
	printf("\n4.Division");
	printf("\n5.Get Remainder");
	printf("\n\nEnter your choice:");
	scanf("%d",&opr);
	switch(opr)
	{
		case 1: printf("\nAddition is : %d",(a+b));
			break;
		case 2: printf("\nSubtraction is : %d",(a-b));
			break;
		case 3: printf("\nMultition is : %d",(a*b));
			break;
		case 4: printf("\nDivision is : %d",(a/b));
			break;
		case 5: printf("\nRemainder is : %d",(a%b));
			break;
		default : printf("\nYou entered wrong number");
			break;
	}

	return 0;

}

C++ program

//A Switch Case Program in C and C++
#include<iostream>

using namespace std;

int main()
{
	int a,b,opr;
	cout<<"\nEnter two numbers:";
	cin>>a>>b;
	cout<<"\nSelect the operation to perform.";
	cout<<"\n1.Addition";
	cout<<"\n2.Subtraction";
	cout<<"\n3.Multiplication";
	cout<<"\n4.Division";
	cout<<"\n5.Get Remainder";
	cout<<"\n\nEnter your choice:";
	cin>>opr;
	switch(opr)
	{
		case 1: cout<<"\nAddition is :"<<(a+b);
			break;
		case 2: cout<<"\nSubtraction is :"<<(a-b);
			break;
		case 3: cout<<"\nMultition is :"<<(a*b);
			break;
		case 4: cout<<"\nDivision is :"<<(a/b);
			break;
		case 5: cout<<"\nRemainder is :"<<(a%b);
			break;
		default : cout<<"\nYou entered wrong number";
			break;
	}

	return 0;

}

2. Output

A Switch Case Program in C and C++

Also read : 10 things you don’t need to have to become a programmer.

3. Program Parts

  1. #include is a pre-processor directive. It is used to include header files.
  2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
  3. conio is a header file which is use to include commands like clrscr(),getch() in the code.
  4. .h is an extension for header file.
  5. int is data type. It indicates that the program returns a value.
  6. main() is main function that indicates the compiler that the user written programs starts from here.
  7. int a,b,opr are the variables names of integer data type.
  8. print/cout is the keyword use to print a message.
  9. switch is keyword in C/C++. It will take value stored in opr and will check with every case inside the switch block.
  10. case is also a keyword which works with switch.
  11. return statement is use to return a value. If any error occurs the program will return 0.

4.Explanation

First the program will accept two numbers. Then it will ask user which operation he/she wants to perform. User’s choice will be stored in opr variables. And now the program will encounter switch statement and will enter switch case. Here program will check opr value with every variable. If it matches with any case it will perform subsequent operation. And if it does not match with any case it will print the message enclosed in default statement i.e. “You entered wrong number”.

Conclusion

That was a switch program in C and C++. We challenge you to perform other operations using switch case. Tell us in the comment section what program you typed using switch case. Also do tell us what other programs or blogs you want us to post. And for daily updates do follow us on instagram.

For more such posts visit again!

ErrorFreeProgram.

8 COMMENTS

comments user
Saurabh Phadke

Nice ๐Ÿ‘Œ๐Ÿ‘Œ

    comments user
    errorfreeprogram

    Thank you for sharing your feedback! Keep visiting.

comments user
Sherin

wow๐Ÿ‘๐Ÿ™๐Ÿ™๐Ÿ‘

    comments user
    ErrorFreeProgram

    Thank for sharing your feedback!

Leave a Reply

Your email address will not be published. Required fields are marked *