-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;
Showing posts with label switch. Show all posts
Showing posts with label switch. Show all posts

Wednesday, 22 July 2015

Convertion of marlas into square feet

My aim is to implement C++ in our daily lives and for this purpose,today I am here with a very easy but interesting example of "switch" statement.This program will convert marlas being enterd by the user into square feet and square feet being enterd by the user into marlas.I have also used the concept of "goto" statement in this program.

Below is the source code of this program.

Source code


#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int choice;
float marlas,squarefeet;
x: //label
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl;
cout<<"Press 1 for converting marlas into square feet."<<endl<<endl;
cout<<"Press 2 for converting square feet into marlas."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
cout<<"Enter the required marlas:";
cin>>marlas;
cout<<endl;
squarefeet=marlas*5399.21;
cout<<"The required square feet="<<squarefeet<<endl<<endl;
break;
case 2:
cout<<"Enter the required square feets:";
cin>>squarefeet;
cout<<endl;
marlas=squarefeet*0.0001;
cout<<"The required marlas="<<marlas<<endl<<endl;
break;
default:
cout<<"Invalid choice."<<endl;
}
goto x;
}

Output

convertion of marlas into square units

Explanation

As we know that the formula for converting marlas into square feet is as follows:

squarefeet=marlas*5399.21;

The formula for converting square feet into marlas is as follows:

marlas=squarefeet*0.0001;

The "goto" statement used in the program will repeat the menu again and again.

Wednesday, 15 April 2015

Height conversion using "switch"

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with another interesting program that will convert the height entered in centimeters into inches and feet using "switch" statement.
As I have discussed in my previous blog that a "switch" statement is used to test different conditions.So, below is the explanation of this program.

Explanation

In this program, I will take the height as input from the user.Then, there would be two conditions before the user.If the user enters "1", then it will convert the height into feet by using the formula,

Feet=Height/30.48


If the user enters "2", then it will convert the height into inches by using the formula,

Inches=Height/2.54.


If the user enters any other number except "1" or "2", it will simply display a message, " Invalid choice !".

Below is the source code of this program.

Source code


Output






Tuesday, 14 April 2015

Calculating the area and circumference by "switch" statement

Hello and Asalam o Alikum. I hope you all are fine and enjoying good health by the grace of Allah Almighty.Today I am here with another example of "switch" statement.

Explanation

 In this program, I will take radius as input from the user.
In the next step, there would be 2 options before the user. If the user enters "1", then it will calculate the area of the circle by the following formula,

Area=3.1416*radius*radius


If the user enters "2", then it will calculate the circumference of the circle by the following formula,

Circumference=2*3.1416*radius 


If the user enters any other number except "1" and "2", it will display a message,"Invalid operator".

So,below is the source code of this interesting program.

Source code


Output



Monday, 13 April 2015

Temperature conversion using "switch" statement

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty .
Today I am here with an important program that is related to temperature conversion using "switch" statement.

Explanation


As I have discussed in my previous blogs that a switch statement is used to test different condition.So,in the below mentioned program, there would be two choices before the user. If the user enters "1",then it will ask him to enter the temperature in celsius. It then converts it into fahrenheit according to the formula

f=c*9/5+32
Suppose the user enters "37" as input.So,
f=37*9/5+32
 =98.599998


. If the user enters "2",then it will ask him to enter the temperature in fahrenheit. It then converts it into celsius according to the formula 

c=(f-32)*5/9
Suppose the user enters "98.599998" as input.So,
c=(98.599998-32)*5/9
 =37


. So,this  is the logic on which this program works.

Below is the source code of this program.

Source code


Output

Saturday, 11 April 2015

Determining a vowel using "switch" statement

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with a new program that will determine a vowel using “switch” statement.We know that vowel characters are:"a,e,i,o,u".
As I have discussed in my previous blog that a “switch” statement is used to test different conditions. So, in the below mentioned program, I will take a character from the user and it will determine whether it is vowel or not.
Suppose I enter “E” as input. As we know that “E” is a vowel, so it will compare it with different cases. If it matches with a certain case, it will display “You entered a vowel.” If it does not matches with any of the case, the statement(s) below the default case will be executed and it will display, “You entered a consonant.”

Below is the source code of this program.

Source code


Output

If you have any ambiguity in understanding the logic of this program, feel free to contact me at "G.A.Mujtaba11@gmail.com".


Friday, 10 April 2015

Making a simple calculator using switch statement

Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with another program that will help you to make a simple arithmetic calculator using switch statement.
Actually switch statement is a good alternative of “if-else-if” or nested if-else. It is very useful in a condition when there are many choices available and only one should be executed.Its syntax is as follows:

Syntax

switch(expression)
{
case 1:
statements;
break;
case 2:
statements;
break;
.
.
default:
statements;
}


Below is the source code of the above mentioned program:

Source code

khgamujtaba,C++,making calculator using switch statement

 Output

khgamujtaba,C++,making calculator using switch statement

Explanation:
               In this program,the result of a single expression is compared with multiple cases.If it matches with a certain case,the statement(s) below that case is executed.After execution,it comes out of the "switch" body due to "break" statement.This is actually the logic of this program.