Create Simple Calculator(perform all arithmetic operation)using Switch Case
<!doctype html>
<html>
<head>
</head>
<body>
<script>
var op =prompt("Enter an
operator (+, -, *,/)");
var
firstNumber=parseInt(prompt("Enter 1st operand"));
var
secondNumber=parseInt(prompt("Enter 2nd operand"));
switch(op)
{
case '+':
alert((firstNumber + secondNumber));
break;
case '-':
alert((firstNumber -
secondNumber));
break;
case '*':
alert((firstNumber *
secondNumber));
break;
case '/':
alert((firstNumber /
secondNumber));
break;
// operator doesn't match (+, -, *, /)
default:
alert("invalid operator");
}
</script>
</body>
</html>
To check vowel or consonant using switch
case
<!doctype
html>
<html>
<head>
</head>
<body>
<script>
var alphabet
=prompt("Enter a Character");
switch(alphabet)
{
case 'a':
alert("Vowel");
break;
case 'e':
alert("Vowel");
break;
case 'i':
alert("Vowel");
break;
case 'o':
alert("Vowel");
break;
case 'u':
alert("Vowel");
break;
case 'A':
alert("Vowel");
break;
case 'E':
alert("Vowel");
break;
case 'I':
alert("Vowel");
break;
case 'O':
alert("Vowel");
break;
case 'U':
alert("Vowel");
break;
default:
alert("Consonant");
}
</script>
</body>
</html>
To print number of
days in a month using switch case
<!doctype
html>
<html>
<head>
</head>
<body>
<script>
var month
=parseInt(prompt("Enter month(1-12)"));
switch(month)
{
case 1:
alert("31 days");
break;
case 2:
alert("28/29 days");
break;
case 3:
alert("31 days");
break;
case 4:
alert("30 days");
break;
case 5:
alert("31 days");
break;
case 6:
alert("30 days");
break;
case 7:
alert("31 days");
break;
case 8:
alert("31 days");
break;
case 9:
alert("30 days");
break;
case 10:
alert("31 days");
break;
case 11:
alert("30 days");
break;
case 12:
alert("31 days");
break;
default:
alert("Invalid month! Please
enter month number between 1-12");
}
</script>
</body>
</html>
To check Even or Odd number using switch case
<!doctype
html>
<html>
<head>
</head>
<body>
<script>
var num
=parseInt(prompt("Enter number"));
switch(num %
2)
{
/* If n%2 == 0 */
case 0:
alert("Number is Even");
break;
/* Else if n%2 == 1 */
case 1:
alert("Number is Odd");
break;
}
</script>
</body>
</html>
To print day of week using switch case
<!doctype
html>
<html>
<head>
</head>
<body>
<script>
var week
=parseInt(prompt("Enter week number(1-7)"));
switch(week)
{
case 1:
alert("Monday");
break;
case 2:
alert("Tuesday");
break;
case 3:
alert("Wednesday");
break;
case 4:
alert("Thursday");
break;
case 5:
alert("Friday");
break;
case 6:
alert("Saturday");
break;
case 7:
alert("Sunday");
break;
default:
alert("Invalid week! Please
enter week number between 1-7.");
}
</script>
</body>
</html>
No comments:
Post a Comment