C program for arithmetic operations using switch case and do while loop

A switch case is used to select one among multiple options.

To perform arithmetic operations, we create case for each operator, for matching operator it will call appropriate case statement and execute the statements.

In our program we will use switch case statement and do while loop statement

You can check both details here

  1. Switch Case Statement
  2. Do While Loop Statement

C program for arithmetic operations using switch case

Lets see steps to write this C program

  1. Declare variables
  2. start loop
  3. get num1 ,operator, num2 value from user
  4. start switch case and pass operator in switch case
  5. write case for arithmetic operators and its associated statements
  6. define default in switch case
  7. Take input from user to check loop continuity
  8. if user enters yes the repeat step 2-7 else end the loop.

Output

Read More

  1. C++ program for student details
  2. Anonymous Object in C++
  3. Lowercase character to uppercase character
  4. Command Line Argument
  5. Local vs Global Object in C++
  6. Local and Nested Classes in C++
  7. Switch Case in C Programming
  8. Constructor in C++
  9. Know more about C++

PHP for and foreach loop statement

It is used when one knows how many time one need to run the particular loop. It is also of two types:

1. PHP for loop statement

Loop back again and again through specified time duration.

PHP for loop syntax:

Example: PHP for loop example

Result:

The number is: 0
The number is: 1
The number is: 2
The number is: 3

2. PHP foreach loop statement

For each elements in the array it give result.
It is used to iterate all the elements over array.

PHP foreach loop syntax:

Example: PHP foreach loop example

Result:
swift
alto
toyoto
I10

PHP do while loop statement with syntax and example

If specified condition is true, loop through block of code once and then repeat again and again.

Syntax of PHP do while loop:

Example:

Result:

Do while loop is known as exit control loop because it check the condition at last(on exit).

Do while loop executes at least once when if condition is false.

PHP Switch Case Statement Syntax and Example

This statement is used if one needs to select one from multiple options.

PHP Switch Case Syntax:

  1. PHP switch case is used to select one among multiple options.
  2. inside switch we can use expression or  value
  3. inside switch there can be multiple case statement with different case value.
  4. case value must be constant.
  5. break is optional in php switch case
  6. Default is optional in php switch
  7. If any case does not match then default is executed.

Example: PHP Switch case statement

Result:
I booked Swift

PHP if else, else if Statements with examples

There are few conditional statements in PHP that is been used for putting different conditions. All the statements are as follows:

1. PHP if statement

It execute that code only if the situation is true otherwise not.
if statement Syntax:

Example:

Result
PHP!

2. PHP if else statement

If true then execute the result, but in case condition false then output will be other coded thing.

PHP if else Syntax:

Example:

Result:
PHP!

3. PHP if…else if….else statement

Sometime first condition get false then to check or test another condition we use this statement.

PHP if…else if….else Syntax:

Example:

Result:

PHP Operators

Operators are used to perform some actions on variables and values. There are 7 types of operators.

1.PHP Arithmetic Operators

This operator is used with numerical values so that one can get value of a particular value.

Arithmetic operators are +, – , * , / , % , **(power)

Syntax:
Addition- $a + $b
Subtraction- $a – $b
Multiplication- $a * $b
Division- $a / $b
Modulus- $a % $b
Exponentiation- $a ** $b

Example: Use of PHP addition(+), subtraction(-), multiplication (*), division (/), modulo (%) and power(**) operators.

2. PHP Assignment Operators

It is used with numerical values when some need to write the value of a particular variable.

Assignment operators are =, +=, -=, *=,  /=, %=, **=

Syntax:

a = b a = b Left operand shifts to right operand
a += b a = a + b Addition
a -= b a = a – b Subtraction
a *= b a = a * b Multiplication
a /= b a = a / b Division
a %= b a = a % b Modulus

Example: Use of PHP Assignment Operators (=, +=, -=, *=, /=, %=, **=)

3. PHP Comparison Operators

== Equal
=== Identical
!= Not equal
<> Not equal
!== Not identical
> Greater than
< Less than >= Greater than or equal to
<= Less than or equal to

Example: Use of PHP equal (==) and  (===) Identical operators.

Example: Use of PHP (!=) Not equal and  (<>) Not equal  and (!==) Not identical operators.

Example: Use of PHP (>) Greater than, (<) Less than (>=) Greater than or equal to (<=) Less than or equal to operators.

4. PHP Increment or Decrement Operator

Increment decrement operators are ++ and –.

++$x Pre-increment
$x++ Post-increment
–$x Pre-decrement
$x– Post-decrement

Example: Use of PHP (++) pre-increment , post increment , (–) pre decrement and post decrement operators.

5. PHP Logical Operators

and, or ,xor, &&, ||, ! are logical operators in PHP

And $x and $y
Or $x or $y
Xor $x xor $y
And $x && $y
Or $x || $y
Not !$x

Example: Use of PHP  (and , &&) and operator.

Result

Example: Use of PHP  (or , ||) and operator.

Example: Use of PHP  (!) not and (xor) xor operator.

6. PHP String Operators

. Concatenation $txt1 . $txt2
.= Concatenation assignment $txt1 .= $txt2

Example: Use of PHP  (.) Concatenation and and (.=) Concatenation assignment operator.

7. PHP Array Operators

It is used to compare arrays with each other

+ Union $x + $y
== Equality $x == $y
=== Identity $x === $y
!= Inequality $x != $y
<> Inequality $x <> $y
!== Non-identity $x !== $y

Example: Use of PHP  array  (+) union operator

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )

Example: Use of PHP  array (==) Equality (===) Identity , (!=) Inequality (<>), Inequality and (!==) Non-identity and php (=>) operators.

Output

PHP Data Types

PHP datatype specifies which type of data a variable can store.

There are following data types of PHP:

  • String
  • Integer
  • Float
  • Boolean
  • Array

1. PHP Strings

This is sequence of characters which can be put in whether double quotes or single quotes.

Example:

Result:

2 PHP Integers

Integers are whole numbers in which there would be no decimal.

It should have at least numeric digit, do not include any comma or blank space, can be positive or negative but not any decimal part and it can only be expressed in decimal, hexadecimal and octal decimal .
Example:

Result:

3 PHP float

This can be presented in decimal format or in exponential form.

In PHP real values are considered as php double, or float or real data type.
Example:

Result:

4 PHP Boolean

boolean php have two optional with them one is true and another is false.

Syntax:
$a=TRUE;
$B=FALSE;

Result

5 PHP Array

It can easily store multiple values in single variable.

Example: Print integer array in PHP using for loop.

Result:

Example: Print String array in PHP using foreach loop.

Output

Echo and print Statements in PHP

There are two types statement in PHP by which we can generate output, one is echo and another is print.

1. Echo in PHP

What is echo in php : echo is a used to print data in output screen.

It can be used with or without the parenthesis.
Example:

Result:

PHP!
I’m human being!
This is new way to learn many new things.

Result:

2. Print in PHP

What is print in php : print is a used to print data in output screen.

PHP echo and PHP print are doing the same work.

It can also be used with or without parenthesis.

Example:

Result:

PHP!
I’m human being!
This is new way to learn many new things.

3 Echo Vs Print in PHP 

They are exactly same working but they both have some similarity that echo do not have any return value but print have return value 1.

And because of that print can be used in expressions.

Echo is very fast as compared to print. It can take multiple arguments but echo can take only one arguments.

how to return array, arraylist, object from a method in java

Returning an Array from Java

In Java we can create methods to do specific work and many times we have to return results from java method.

A java method can return any datatypes or object type from method.

1 Return primitive datatypes from method

A Returning int long float double values from a java method

A. Return a integer value from a Method in java

Lets see a simple program to return an maximum integer from method.

Output

To return any value from method you have to specify return type of method.

and in method provide a return statement to return specific value from method.

Extending the above program for other data types

B Return a integer, long, float, double values from java method

Output

C Returning char and String value from a method in java

2 Returning an Array

A Returning an integer array from a method

Lets see returning a value from method using one example below.

Q: Write a Program to create a method to sort integer array in java

Output

B. Java method to return double array from a Java method

Output

C Return a String array from a method in Java

Here we will take names in array and sort then in ascending order Lets see How to sort Alphabetically in java

Output

D Return character array from a method in java

Here we will pass a string to a method and convert it in to character array and return that character array to main method.

Output

E Return two dimensional array in java

Output

F Return List from a method in java

Here we have created List of integers and passed in a method getSubList().

This method generate sub list by checking value of list.

If element is greater that 50 it will add element to new list and return the new list.

On main method we get sublist and print it.

How to remove special characters from a string in java

Here we will see how you can remove special characters from given string.

In Java String is a class which provides different constructors and methods to manipulate strings.

To remove special characters we will use String class method replaceAll().

String replaceAll(String regex, String replacement)

This method takes two argument.

First is String regular expression (regex) that matched the substring which user want to replace.

Second is String replacement, the string which will replace with regular expression substring.

1. Removing specific special characters

Output

In above program we know that special characters [email protected],^ are available in String.

2. Removing all special characters

Some times we don’t know which special character is there.

To remove all special characters from string we use

string.replaceAll("[^a-zA-Z]+"," ");

It will remove all special characters except small a-z and A-Z.

Output

It remove characters other then small and capital alphabets.

Here you can see there is a digit in a String that is also removed.

3. Removing special characters except digits

It you want not to remove digits then you have to modify your Regular expression.

Output

To achieve above two results we can also use this

replaceAll("[^a-zA-Z0]+"," "); is same as replaceAll("[^\\p{Alpha}]+","");

replaceAll("[^a-zA-Z0-9]+"," "); is same as replaceAll("[^\\p{Alpha}\\p{Digit}]+","");

Output

4. Allow few special characters in string

Some times we want to keep some special characters with string like comma(,) or fullstop (.).

Other then this we want to remove.

For that we have to add these two in our regular expression as below

replaceAll("[^a-zA-Z0-9,.]+"," ");

Output