April 14, 2021

# Common Programming Interview questions and Answers

No, any technical interview can be completed without logical questions based on simple programming techniques. When you are going for a Programmer profile, you must know the simple logic applied to make complex programs. Here is the collection of such important logical questions asked in an interview.These collection includes reverse string,factorial,star printing pattern,swap,leap year,prime number,odd-even number,palindrome etc.

Write PHP script to reverse string without using any PHP function.

```
function reverseString(\$string){
\$i=0;
\$output_string='';
while(\$string[\$i]){
\$output_string=\$string[\$i].\$output_string;
\$i++;
}
return \$output;
}
\$my_string="this is a string to be reversed";
\$output=reverseString(\$my_string);

```

Write a program to find Factorial Of a number without using recursion.

```
function findFactorial(\$n){
\$factorial = 1;
if(\$n&amp;lt;=1){
return \$factorial;
}else if(\$n&amp;gt;1){
\$i=0;
while(\$n-\$i){
\$factorial = \$factorial*(\$n-\$i);
\$i++;
}
}

return \$factorial;

}
echo findFactorial(10);

```

Write a program to find Factorial of a number using recursion.

```
function findFactorialRec(\$number){
if(\$number &amp;lt;= 1){
return 1;
}
else{
return \$number * findFactorialRec(\$number - 1);
}
}
echo findFactorialRec(10);

```

Write a program to print the following Pattern

*
*  *
*  *  *
*  *  *  *
*  *  *  *  *

```
\$n=5;
for (\$i=1;\$i&amp;lt;=\$n;\$i++){
for (\$j=0; \$j &amp;lt; \$i ; \$j++) {
echo "# ";
}
echo "&amp;lt;br&amp;gt;";
}
echo "&amp;lt;br&amp;gt;";

```

Write a program to print the following Pattern.

*
*  *
*  *  *
*  *  *  *
*  *  *  *  *

```
for (\$i=1;\$i&amp;lt;=\$n;\$i++){
for (\$j=\$n-\$i; \$j &amp;gt; 0 ; \$j--) {
echo "+";
}
for (\$k=0; \$k &amp;lt; \$i ; \$k++) {
echo "*";
}
echo "&amp;lt;br&amp;gt;";
}
echo "&amp;lt;br&amp;gt;";

```

Write a program to print the following Pattern.

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

```
\$n=5;
for (\$i=0; \$i &amp;lt;\$n-1 ; \$i++) {
for(\$j=\$n-\$i;\$j&amp;gt;0;\$j--){
echo "+";
}
for(\$k=1;\$k&amp;lt;=(2*\$i)+1;\$k++){
echo "*";
}
echo "&amp;lt;br&amp;gt;";
}
echo "&amp;lt;br&amp;gt;";

```

Write a function to check if a string is a palindrome or not.

```
function checkPalindrome(\$string){
\$length=strlen(\$string);
if(\$length&amp;gt;1){
\$mid = floor(\$length/2);
\$pal=true;
for (\$i=0; \$i &amp;lt;=\$mid; \$i++) {
if(\$string[\$i]!=\$string[\$length-(\$i+1)]){
\$pal=false;
break;
}
}
if(\$pal==false){
return "NOT A PALINDROM";
}else{
return "PALINDROM";
}
}
}

\$string ="ABCDEfEDCBA";
echo checkPalindrome(\$string);

```

Write a program to check whether a given number is prime or not:

```
function checkPrime(\$number){
\$check=0;
for(\$i=2;\$i&amp;lt;=(\$number/2);\$i++)
{
if(\$number%\$i==0){
\$check++;
if(\$check==1){
break ;
}
}
}
if(\$check==0){
echo "\$number is a Prime Number";
}else{
echo "\$number is not a Prime Number";
}
}

```

Write a function to test a given year is Leap Year:

```
function checkLeapYear(\$year){
if(\$year%4==0){
echo "\$year is a leap year";
}else{
echo "\$year is not a leap year";
}
}

```

Write a program to swap two numbers without using any temporary variable.

```
function swapNumbers(\$a,\$b){
\$a=\$a+\$b;
\$b=\$a-\$b;
\$a=\$a-\$b;
echo "Now A is \$a and B is \$b";
}

```

Write a program to check that a given number is odd or even.

```
function checkOddEven(\$number){
if(\$number % 2 == 0){
echo "\$number is EVEN";
}
else{
echo "\$number is ODD";
}
}

```