Switch Case Practice in C++ | Part 2

Here is Video of Switch case Practice in C++ Part 2.

Task 1

Write a Program that inputs a value and type of conversion. The Program should then output the value after conversion. the program should include the following conversions:

1 inch = 2.54 centimeters
1 gallon = 3.785 liters
1 mile = 1.609 Kilometers
1 pound = 0.4536 Kilograms

Code

#include<iostream>
using namespace std;
int main()
{
	int type;
	float value;
	cout<<"Chose any one for conversion : \n";
	cout<<"1- Centimeters \n";
	cout<<"2- Liters \n";
	cout<<"3- Kilometers \n";
	cout<<"4- KiloGrams\n";
	cin>>type;
	cout<<"\nEnter Value : ";
	cin>>value;
	cout<<"\n";
	switch(type)
	{
		case 1:
			cout<<value <<" inches = "<<value*2.54<<" cm ";
			break;
		case 2:
			cout<<value <<" gallon = "<<value*3.785<<" liters ";
			break;
		case 3:
			cout<<value <<" miles = "<<value*1.609<<" km ";
			break;
		case 4:
			cout<<value <<" pounds = "<<value*0.4536<<" kg ";
			break;
		default :
			cout<<" Invalid Conversion :(";
	}
}

Output

Chose any one for conversion :
1- Centimeters
2- Liters
3- Kilometers
4- KiloGrams
3

Enter Value : 1234

1234 miles = 1985.51 km

Task 2

Write a program that ask the user to select the shape whose are they want to find and then find the area of the shape selected by the user… ask the user to enter 1 for Circle,2 for triangle 3 for square.

Code

#include<iostream>
using namespace std;
int main()
{
	int ch;
	cout<<"Select Shape \n\n";
	cout<<"1- Circle \n";
	cout<<"2- Triangle \n";
	cout<<"3- Square ";
	cin>>ch;
	switch(ch)
	{
		//for circle
		case 1:
			float rc;//radius of circle
			cout<<"\n\tEnter Radius : ";
			cin>>rc;
			cout<<"\nArea of Circle with Radius "<<rc<<" = "<<3.1416*rc*rc;
			break;
		//for triangle
		case 2:
			float ht,wt;//hieght and width;
			cout<<"\n\tEnter Height and width : ";
			cin>>ht>>wt;
			cout<<"\nArea of Triangle is "<<0.5*ht*wt;
			break;
		//for square
		case 3:
			float ls;//length
			cout<<"\n\tEnter Length : ";
			cin>>ls;
			cout<<"\nArea of Square with Length "<<ls<<" = "<<ls*ls;
			break;
		default:
			cout<<"Invalid Choice ";
	}
}

Output

Select Shape

1- Circle
2- Triangle
3- Square
3

        Enter Length : 7

Area of Square with Length 7 = 49

Related Post

Leave a Reply

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